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