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