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