Chris Lattner | 566b6ce | 2007-08-24 02:22:53 +0000 | [diff] [blame] | 1 | //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===// |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Function.h" |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 25 | // Complex Expression Emitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 28 | typedef CodeGenFunction::ComplexPairTy ComplexPairTy; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 31 | class ComplexExprEmitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 32 | : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> { |
| 33 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 34 | CGBuilderTy &Builder; |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 35 | // True is we should ignore the value of a |
| 36 | bool IgnoreReal; |
| 37 | bool IgnoreImag; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 38 | public: |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 39 | ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false) |
| 40 | : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 43 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 44 | //===--------------------------------------------------------------------===// |
| 45 | // Utilities |
| 46 | //===--------------------------------------------------------------------===// |
| 47 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 48 | 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 Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 58 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 59 | /// 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 Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 62 | ComplexPairTy EmitLoadOfLValue(const Expr *E) { |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 63 | return EmitLoadOfLValue(CGF.EmitLValue(E)); |
| 64 | } |
| 65 | |
| 66 | ComplexPairTy EmitLoadOfLValue(LValue LV) { |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 67 | if (LV.isSimple()) |
| 68 | return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified()); |
| 69 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 70 | assert(LV.isPropertyRef() && "Unknown LValue type!"); |
| 71 | return CGF.EmitLoadOfPropertyRefLValue(LV).getComplexVal(); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 72 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 74 | /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load |
| 75 | /// the real and imaginary pieces. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 76 | ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 77 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 78 | /// 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 Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 88 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 89 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 90 | void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 91 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 92 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 93 | ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType, |
| 94 | QualType DestType); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 95 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 96 | //===--------------------------------------------------------------------===// |
| 97 | // Visitor Methods |
| 98 | //===--------------------------------------------------------------------===// |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 99 | |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 100 | ComplexPairTy Visit(Expr *E) { |
John McCall | cf0b2d8 | 2011-02-17 19:02:56 +0000 | [diff] [blame] | 101 | return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E); |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 104 | ComplexPairTy VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 105 | S->dump(CGF.getContext().getSourceManager()); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 106 | assert(0 && "Stmt can't have complex result type!"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 107 | return ComplexPairTy(); |
| 108 | } |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 109 | ComplexPairTy VisitExpr(Expr *S); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 110 | ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());} |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame^] | 111 | ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 112 | return Visit(GE->getResultExpr()); |
| 113 | } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 114 | ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 115 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 116 | // l-values. |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 117 | ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 118 | ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 119 | return EmitLoadOfLValue(E); |
| 120 | } |
| 121 | ComplexPairTy VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 122 | assert(E->getObjectKind() == OK_Ordinary); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 123 | return EmitLoadOfLValue(E); |
| 124 | } |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 125 | ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 126 | return CGF.EmitObjCMessageExpr(E).getComplexVal(); |
| 127 | } |
Chris Lattner | 8bcd723 | 2007-08-21 18:03:58 +0000 | [diff] [blame] | 128 | ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 129 | ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 130 | ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 131 | if (E->isGLValue()) |
| 132 | return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E)); |
| 133 | return CGF.getOpaqueRValueMapping(E).getComplexVal(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 135 | |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 136 | // FIXME: CompoundLiteralExpr |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 138 | ComplexPairTy EmitCast(CastExpr::CastKind CK, Expr *Op, QualType DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 139 | ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 140 | // Unlike for scalars, we don't have to worry about function->ptr demotion |
| 141 | // here. |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 142 | return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 143 | } |
| 144 | ComplexPairTy VisitCastExpr(CastExpr *E) { |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 145 | return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 147 | ComplexPairTy VisitCallExpr(const CallExpr *E); |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 148 | ComplexPairTy VisitStmtExpr(const StmtExpr *E); |
| 149 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 150 | // Operators. |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 151 | ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 152 | bool isInc, bool isPre) { |
| 153 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 154 | return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre); |
| 155 | } |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 156 | ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) { |
| 157 | return VisitPrePostIncDec(E, false, false); |
| 158 | } |
| 159 | ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) { |
| 160 | return VisitPrePostIncDec(E, true, false); |
| 161 | } |
| 162 | ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) { |
| 163 | return VisitPrePostIncDec(E, false, true); |
| 164 | } |
| 165 | ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) { |
| 166 | return VisitPrePostIncDec(E, true, true); |
| 167 | } |
| 168 | ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 169 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 170 | TestAndClearIgnoreReal(); |
| 171 | TestAndClearIgnoreImag(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 172 | return Visit(E->getSubExpr()); |
| 173 | } |
| 174 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 175 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 176 | // LNot,Real,Imag never return complex. |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 177 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 178 | return Visit(E->getSubExpr()); |
| 179 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 180 | ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 181 | return Visit(DAE->getExpr()); |
| 182 | } |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 183 | ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) { |
| 184 | return CGF.EmitExprWithCleanups(E).getComplexVal(); |
Anders Carlsson | 4b76b4f | 2009-05-31 00:12:05 +0000 | [diff] [blame] | 185 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 186 | ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 187 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 188 | QualType Elem = E->getType()->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 189 | llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 190 | return ComplexPairTy(Null, Null); |
| 191 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 192 | ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 193 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 194 | QualType Elem = E->getType()->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 195 | llvm::Constant *Null = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 196 | llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 197 | return ComplexPairTy(Null, Null); |
| 198 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 199 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 200 | struct BinOpInfo { |
| 201 | ComplexPairTy LHS; |
| 202 | ComplexPairTy RHS; |
| 203 | QualType Ty; // Computation Type. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 206 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 207 | LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 208 | ComplexPairTy (ComplexExprEmitter::*Func) |
| 209 | (const BinOpInfo &), |
| 210 | ComplexPairTy &Val); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 211 | ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E, |
| 212 | ComplexPairTy (ComplexExprEmitter::*Func) |
| 213 | (const BinOpInfo &)); |
| 214 | |
| 215 | ComplexPairTy EmitBinAdd(const BinOpInfo &Op); |
| 216 | ComplexPairTy EmitBinSub(const BinOpInfo &Op); |
| 217 | ComplexPairTy EmitBinMul(const BinOpInfo &Op); |
| 218 | ComplexPairTy EmitBinDiv(const BinOpInfo &Op); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 219 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 220 | ComplexPairTy VisitBinAdd(const BinaryOperator *E) { |
| 221 | return EmitBinAdd(EmitBinOps(E)); |
| 222 | } |
| 223 | ComplexPairTy VisitBinSub(const BinaryOperator *E) { |
| 224 | return EmitBinSub(EmitBinOps(E)); |
| 225 | } |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 226 | ComplexPairTy VisitBinMul(const BinaryOperator *E) { |
| 227 | return EmitBinMul(EmitBinOps(E)); |
| 228 | } |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 229 | ComplexPairTy VisitBinDiv(const BinaryOperator *E) { |
| 230 | return EmitBinDiv(EmitBinOps(E)); |
| 231 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 232 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 233 | // Compound assignments. |
| 234 | ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) { |
| 235 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd); |
| 236 | } |
| 237 | ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) { |
| 238 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub); |
| 239 | } |
| 240 | ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) { |
| 241 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul); |
| 242 | } |
| 243 | ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) { |
| 244 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv); |
| 245 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 612c40c | 2007-08-26 21:27:07 +0000 | [diff] [blame] | 247 | // GCC rejects rem/and/or/xor for integer complex. |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 248 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 249 | |
| 250 | // No comparisons produce a complex result. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 251 | |
| 252 | LValue EmitBinAssignLValue(const BinaryOperator *E, |
| 253 | ComplexPairTy &Val); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 254 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 255 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 256 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 257 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 258 | ComplexPairTy |
| 259 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 260 | ComplexPairTy VisitChooseExpr(ChooseExpr *CE); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 261 | |
| 262 | ComplexPairTy VisitInitListExpr(InitListExpr *E); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 263 | |
| 264 | ComplexPairTy VisitVAArgExpr(VAArgExpr *E); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 265 | }; |
| 266 | } // end anonymous namespace. |
| 267 | |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | // Utilities |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 272 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 273 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 274 | ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr, |
| 275 | bool isVolatile) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 276 | llvm::Value *Real=0, *Imag=0; |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 277 | |
John McCall | ff624c1 | 2010-11-14 09:40:28 +0000 | [diff] [blame] | 278 | if (!IgnoreReal || isVolatile) { |
Benjamin Kramer | 56a44f7 | 2009-11-29 19:51:45 +0000 | [diff] [blame] | 279 | llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0, |
| 280 | SrcPtr->getName() + ".realp"); |
| 281 | Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr->getName() + ".real"); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 282 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 283 | |
John McCall | ff624c1 | 2010-11-14 09:40:28 +0000 | [diff] [blame] | 284 | if (!IgnoreImag || isVolatile) { |
Benjamin Kramer | 56a44f7 | 2009-11-29 19:51:45 +0000 | [diff] [blame] | 285 | llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1, |
| 286 | SrcPtr->getName() + ".imagp"); |
| 287 | Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr->getName() + ".imag"); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 288 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 289 | return ComplexPairTy(Real, Imag); |
| 290 | } |
| 291 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 292 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 293 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 294 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 295 | bool isVolatile) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 296 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real"); |
| 297 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 299 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 300 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
| 304 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
| 306 | // Visitor Methods |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 309 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 310 | CGF.ErrorUnsupported(E, "complex expression"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 311 | const llvm::Type *EltTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 312 | CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType()); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 313 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 314 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 317 | ComplexPairTy ComplexExprEmitter:: |
| 318 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 319 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
John McCall | 8d3d6c9 | 2011-01-13 02:03:06 +0000 | [diff] [blame] | 320 | return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 324 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 325 | if (E->getCallReturnType()->isReferenceType()) |
| 326 | return EmitLoadOfLValue(E); |
| 327 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 328 | return CGF.EmitCallExpr(E).getComplexVal(); |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 331 | ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 332 | CodeGenFunction::StmtExprEvaluation eval(CGF); |
| 333 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal(); |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 336 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 337 | ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, |
| 338 | QualType SrcType, |
| 339 | QualType DestType) { |
| 340 | // Get the src/dest element type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 341 | SrcType = SrcType->getAs<ComplexType>()->getElementType(); |
| 342 | DestType = DestType->getAs<ComplexType>()->getElementType(); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 343 | |
Daniel Dunbar | 56f0d16 | 2009-01-29 06:44:03 +0000 | [diff] [blame] | 344 | // C99 6.3.1.6: When a value of complex type is converted to another |
Daniel Dunbar | e12d8e3 | 2009-01-26 18:02:34 +0000 | [diff] [blame] | 345 | // complex type, both the real and imaginary parts follow the conversion |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 346 | // rules for the corresponding real types. |
| 347 | Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType); |
| 348 | Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType); |
| 349 | return Val; |
| 350 | } |
| 351 | |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 352 | ComplexPairTy ComplexExprEmitter::EmitCast(CastExpr::CastKind CK, Expr *Op, |
| 353 | QualType DestTy) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 354 | switch (CK) { |
| 355 | case CK_GetObjCProperty: { |
| 356 | LValue LV = CGF.EmitLValue(Op); |
| 357 | assert(LV.isPropertyRef() && "Unknown LValue type!"); |
| 358 | return CGF.EmitLoadOfPropertyRefLValue(LV).getComplexVal(); |
| 359 | } |
| 360 | |
| 361 | case CK_NoOp: |
| 362 | case CK_LValueToRValue: |
| 363 | return Visit(Op); |
| 364 | |
| 365 | // TODO: do all of these |
| 366 | default: |
| 367 | break; |
| 368 | } |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 369 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 370 | // Two cases here: cast from (complex to complex) and (scalar to complex). |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 371 | if (Op->getType()->isAnyComplexType()) |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 372 | return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 374 | // FIXME: We should be looking at all of the cast kinds here, not |
| 375 | // cherry-picking the ones we have test cases for. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 376 | if (CK == CK_LValueBitCast) { |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 377 | llvm::Value *V = CGF.EmitLValue(Op).getAddress(); |
| 378 | V = Builder.CreateBitCast(V, |
| 379 | CGF.ConvertType(CGF.getContext().getPointerType(DestTy))); |
| 380 | // FIXME: Are the qualifiers correct here? |
| 381 | return EmitLoadOfComplex(V, DestTy.isVolatileQualified()); |
| 382 | } |
| 383 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 384 | // C99 6.3.1.7: When a value of real type is converted to a complex type, the |
Eli Friedman | c110c54 | 2009-03-23 04:08:46 +0000 | [diff] [blame] | 385 | // real part of the complex result value is determined by the rules of |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 386 | // conversion to the corresponding real type and the imaginary part of the |
| 387 | // complex result value is a positive zero or an unsigned zero. |
| 388 | llvm::Value *Elt = CGF.EmitScalarExpr(Op); |
| 389 | |
| 390 | // Convert the input element to the element type of the complex. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 391 | DestTy = DestTy->getAs<ComplexType>()->getElementType(); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 392 | Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 393 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 394 | // Return (realval, 0). |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 395 | return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType())); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 398 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 399 | TestAndClearIgnoreReal(); |
| 400 | TestAndClearIgnoreImag(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 401 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 403 | llvm::Value *ResR, *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 404 | if (Op.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 405 | ResR = Builder.CreateFNeg(Op.first, "neg.r"); |
| 406 | ResI = Builder.CreateFNeg(Op.second, "neg.i"); |
| 407 | } else { |
| 408 | ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 409 | ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 410 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 411 | return ComplexPairTy(ResR, ResI); |
| 412 | } |
| 413 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 414 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 415 | TestAndClearIgnoreReal(); |
| 416 | TestAndClearIgnoreImag(); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 417 | // ~(a+ib) = a + i*-b |
| 418 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 419 | llvm::Value *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 420 | if (Op.second->getType()->isFloatingPointTy()) |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 421 | ResI = Builder.CreateFNeg(Op.second, "conj.i"); |
| 422 | else |
| 423 | ResI = Builder.CreateNeg(Op.second, "conj.i"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 424 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 425 | return ComplexPairTy(Op.first, ResI); |
| 426 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 427 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 428 | ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 429 | llvm::Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 430 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 431 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 432 | ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 433 | ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 434 | } else { |
| 435 | ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 436 | ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 437 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 438 | return ComplexPairTy(ResR, ResI); |
| 439 | } |
| 440 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 441 | ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 442 | llvm::Value *ResR, *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 443 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 444 | ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 445 | ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 446 | } else { |
| 447 | ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 448 | ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 449 | } |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 450 | return ComplexPairTy(ResR, ResI); |
| 451 | } |
| 452 | |
| 453 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 454 | ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 455 | using llvm::Value; |
| 456 | Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 457 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 458 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 459 | Value *ResRl = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 460 | Value *ResRr = Builder.CreateFMul(Op.LHS.second, Op.RHS.second,"mul.rr"); |
| 461 | ResR = Builder.CreateFSub(ResRl, ResRr, "mul.r"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 463 | Value *ResIl = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 464 | Value *ResIr = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir"); |
| 465 | ResI = Builder.CreateFAdd(ResIl, ResIr, "mul.i"); |
| 466 | } else { |
| 467 | Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 468 | Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr"); |
| 469 | ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 471 | Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 472 | Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir"); |
| 473 | ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
| 474 | } |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 475 | return ComplexPairTy(ResR, ResI); |
| 476 | } |
| 477 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 478 | ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) { |
| 479 | llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second; |
| 480 | llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 482 | |
| 483 | llvm::Value *DSTr, *DSTi; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 484 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 485 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
| 486 | llvm::Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr, "tmp"); // a*c |
| 487 | llvm::Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi, "tmp"); // b*d |
| 488 | llvm::Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2, "tmp"); // ac+bd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 490 | llvm::Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr, "tmp"); // c*c |
| 491 | llvm::Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi, "tmp"); // d*d |
| 492 | llvm::Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5, "tmp"); // cc+dd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 493 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 494 | llvm::Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr, "tmp"); // b*c |
| 495 | llvm::Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi, "tmp"); // a*d |
| 496 | llvm::Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8, "tmp"); // bc-ad |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 498 | DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp"); |
| 499 | DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp"); |
| 500 | } else { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 501 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
| 502 | llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c |
| 503 | llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d |
| 504 | llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 506 | llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c |
| 507 | llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d |
| 508 | llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 510 | llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c |
| 511 | llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d |
| 512 | llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 513 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 514 | if (Op.Ty->getAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) { |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 515 | DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp"); |
| 516 | DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp"); |
| 517 | } else { |
| 518 | DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp"); |
| 519 | DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp"); |
| 520 | } |
| 521 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 523 | return ComplexPairTy(DSTr, DSTi); |
| 524 | } |
| 525 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 526 | ComplexExprEmitter::BinOpInfo |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 527 | ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 528 | TestAndClearIgnoreReal(); |
| 529 | TestAndClearIgnoreImag(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 530 | BinOpInfo Ops; |
| 531 | Ops.LHS = Visit(E->getLHS()); |
| 532 | Ops.RHS = Visit(E->getRHS()); |
| 533 | Ops.Ty = E->getType(); |
| 534 | return Ops; |
| 535 | } |
| 536 | |
| 537 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 538 | LValue ComplexExprEmitter:: |
| 539 | EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 540 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&), |
| 541 | ComplexPairTy &Val) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 542 | TestAndClearIgnoreReal(); |
| 543 | TestAndClearIgnoreImag(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 544 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 545 | |
| 546 | BinOpInfo OpInfo; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 547 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 548 | // Load the RHS and LHS operands. |
| 549 | // __block variables need to have the rhs evaluated first, plus this should |
John McCall | 641ca7d | 2010-11-16 05:45:35 +0000 | [diff] [blame] | 550 | // improve codegen a little. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 551 | OpInfo.Ty = E->getComputationResultType(); |
John McCall | 641ca7d | 2010-11-16 05:45:35 +0000 | [diff] [blame] | 552 | |
| 553 | // The RHS should have been converted to the computation type. |
| 554 | assert(OpInfo.Ty->isAnyComplexType()); |
| 555 | assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty, |
| 556 | E->getRHS()->getType())); |
| 557 | OpInfo.RHS = Visit(E->getRHS()); |
Fariborz Jahanian | dfb1eb2 | 2010-03-23 18:08:50 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 559 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 560 | |
| 561 | // Load from the l-value. |
| 562 | ComplexPairTy LHSComplexPair = EmitLoadOfLValue(LHS); |
Fariborz Jahanian | dfb1eb2 | 2010-03-23 18:08:50 +0000 | [diff] [blame] | 563 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 564 | OpInfo.LHS = EmitComplexToComplexCast(LHSComplexPair, LHSTy, OpInfo.Ty); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 565 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 566 | // Expand the binary operator. |
| 567 | ComplexPairTy Result = (this->*Func)(OpInfo); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 568 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 569 | // Truncate the result back to the LHS type. |
| 570 | Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 571 | Val = Result; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 572 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 573 | // Store the result value into the LHS lvalue. |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 574 | EmitStoreThroughLValue(Result, LHS); |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 575 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 576 | return LHS; |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 577 | } |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 578 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 579 | // Compound assignments. |
| 580 | ComplexPairTy ComplexExprEmitter:: |
| 581 | EmitCompoundAssign(const CompoundAssignOperator *E, |
| 582 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ |
| 583 | ComplexPairTy Val; |
| 584 | LValue LV = EmitCompoundAssignLValue(E, Func, Val); |
| 585 | |
| 586 | // The result of an assignment in C is the assigned r-value. |
| 587 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 588 | return Val; |
| 589 | |
| 590 | // Objective-C property assignment never reloads the value following a store. |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 591 | if (LV.isPropertyRef()) |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 592 | return Val; |
| 593 | |
| 594 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 595 | if (!LV.isVolatileQualified()) |
| 596 | return Val; |
| 597 | |
| 598 | return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified()); |
| 599 | } |
| 600 | |
| 601 | LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E, |
| 602 | ComplexPairTy &Val) { |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 603 | assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), |
| 604 | E->getRHS()->getType()) && |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 605 | "Invalid assignment"); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 606 | TestAndClearIgnoreReal(); |
| 607 | TestAndClearIgnoreImag(); |
| 608 | |
John McCall | cd940a1 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 609 | // Emit the RHS. __block variables need the RHS evaluated first. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 610 | Val = Visit(E->getRHS()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 611 | |
| 612 | // Compute the address to store into. |
| 613 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 614 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 615 | // Store the result value into the LHS lvalue. |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 616 | EmitStoreThroughLValue(Val, LHS); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 617 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 618 | return LHS; |
| 619 | } |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 620 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 621 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 622 | ComplexPairTy Val; |
| 623 | LValue LV = EmitBinAssignLValue(E, Val); |
| 624 | |
| 625 | // The result of an assignment in C is the assigned r-value. |
| 626 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 627 | return Val; |
| 628 | |
| 629 | // Objective-C property assignment never reloads the value following a store. |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 630 | if (LV.isPropertyRef()) |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 631 | return Val; |
| 632 | |
| 633 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 634 | if (!LV.isVolatileQualified()) |
| 635 | return Val; |
| 636 | |
| 637 | return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 640 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 641 | CGF.EmitIgnoredExpr(E->getLHS()); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 642 | return Visit(E->getRHS()); |
| 643 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 644 | |
| 645 | ComplexPairTy ComplexExprEmitter:: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 646 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 647 | TestAndClearIgnoreReal(); |
| 648 | TestAndClearIgnoreImag(); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 649 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 650 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 651 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 652 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 653 | // Bind the common expression if necessary. |
| 654 | CodeGenFunction::OpaqueValueMapping binding(CGF, E); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 655 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 656 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
| 657 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 658 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 659 | eval.begin(CGF); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 660 | CGF.EmitBlock(LHSBlock); |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 661 | ComplexPairTy LHS = Visit(E->getTrueExpr()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 662 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 663 | CGF.EmitBranch(ContBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 664 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 665 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 666 | eval.begin(CGF); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 667 | CGF.EmitBlock(RHSBlock); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 668 | ComplexPairTy RHS = Visit(E->getFalseExpr()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 669 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 670 | CGF.EmitBlock(ContBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 671 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 672 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 673 | // Create a PHI node for the real part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 674 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 675 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 676 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 677 | |
| 678 | // Create a PHI node for the imaginary part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 679 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 680 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 681 | ImagPN->addIncoming(RHS.second, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 682 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 683 | return ComplexPairTy(RealPN, ImagPN); |
| 684 | } |
| 685 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 686 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 687 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 690 | ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 691 | bool Ignore = TestAndClearIgnoreReal(); |
| 692 | (void)Ignore; |
| 693 | assert (Ignore == false && "init list ignored"); |
| 694 | Ignore = TestAndClearIgnoreImag(); |
| 695 | (void)Ignore; |
| 696 | assert (Ignore == false && "init list ignored"); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 697 | if (E->getNumInits()) |
| 698 | return Visit(E->getInit(0)); |
| 699 | |
| 700 | // Empty init list intializes to null |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 701 | QualType Ty = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 702 | const llvm::Type* LTy = CGF.ConvertType(Ty); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 703 | llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 704 | return ComplexPairTy(zeroConstant, zeroConstant); |
| 705 | } |
| 706 | |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 707 | ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 708 | llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 709 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType()); |
| 710 | |
| 711 | if (!ArgPtr) { |
| 712 | CGF.ErrorUnsupported(E, "complex va_arg expression"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 713 | const llvm::Type *EltTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 714 | CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 715 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 716 | return ComplexPairTy(U, U); |
| 717 | } |
| 718 | |
| 719 | // FIXME Volatility. |
| 720 | return EmitLoadOfComplex(ArgPtr, false); |
| 721 | } |
| 722 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 723 | //===----------------------------------------------------------------------===// |
| 724 | // Entry Point into this File |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | |
| 727 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 728 | /// complex type, ignoring the result. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 729 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal, |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 730 | bool IgnoreImag) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 731 | assert(E && E->getType()->isAnyComplexType() && |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 732 | "Invalid complex expression to emit"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 733 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 734 | return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag) |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 735 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 738 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 739 | /// of complex type, storing into the specified Value*. |
| 740 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 741 | llvm::Value *DestAddr, |
| 742 | bool DestIsVolatile) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 743 | assert(E && E->getType()->isAnyComplexType() && |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 744 | "Invalid complex expression to emit"); |
| 745 | ComplexExprEmitter Emitter(*this); |
| 746 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 747 | Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 748 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 749 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 750 | /// StoreComplexToAddr - Store a complex number into the specified address. |
| 751 | void CodeGenFunction::StoreComplexToAddr(ComplexPairTy V, |
| 752 | llvm::Value *DestAddr, |
| 753 | bool DestIsVolatile) { |
| 754 | ComplexExprEmitter(*this).EmitStoreOfComplex(V, DestAddr, DestIsVolatile); |
| 755 | } |
| 756 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 757 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 758 | ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr, |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 759 | bool SrcIsVolatile) { |
| 760 | return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile); |
| 761 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 762 | |
| 763 | LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 764 | assert(E->getOpcode() == BO_Assign); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 765 | ComplexPairTy Val; // ignored |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 766 | return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val); |
| 767 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 768 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 769 | LValue CodeGenFunction:: |
| 770 | EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) { |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 771 | ComplexPairTy(ComplexExprEmitter::*Op)(const ComplexExprEmitter::BinOpInfo &); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 772 | switch (E->getOpcode()) { |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 773 | case BO_MulAssign: Op = &ComplexExprEmitter::EmitBinMul; break; |
| 774 | case BO_DivAssign: Op = &ComplexExprEmitter::EmitBinDiv; break; |
| 775 | case BO_SubAssign: Op = &ComplexExprEmitter::EmitBinSub; break; |
| 776 | case BO_AddAssign: Op = &ComplexExprEmitter::EmitBinAdd; break; |
| 777 | |
| 778 | default: |
| 779 | llvm_unreachable("unexpected complex compound assignment"); |
| 780 | Op = 0; |
| 781 | } |
| 782 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 783 | ComplexPairTy Val; // ignored |
| 784 | return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 785 | } |