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 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 49 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 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(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 85 | |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 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 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +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); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 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); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +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); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 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); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +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); } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 117 | ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 118 | return EmitLoadOfLValue(E); |
| 119 | } |
| 120 | ComplexPairTy VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
| 121 | return EmitLoadOfLValue(E); |
| 122 | } |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 123 | ComplexPairTy VisitObjCImplicitSetterGetterRefExpr( |
| 124 | ObjCImplicitSetterGetterRefExpr *E) { |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 125 | return EmitLoadOfLValue(E); |
| 126 | } |
| 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 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 135 | ComplexPairTy EmitCast(Expr *Op, QualType DestTy); |
| 136 | ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 137 | // Unlike for scalars, we don't have to worry about function->ptr demotion |
| 138 | // here. |
| 139 | return EmitCast(E->getSubExpr(), E->getType()); |
| 140 | } |
| 141 | ComplexPairTy VisitCastExpr(CastExpr *E) { |
| 142 | return EmitCast(E->getSubExpr(), E->getType()); |
| 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, |
| 149 | bool isInc, bool isPre); |
| 150 | ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) { |
| 151 | return VisitPrePostIncDec(E, false, false); |
| 152 | } |
| 153 | ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) { |
| 154 | return VisitPrePostIncDec(E, true, false); |
| 155 | } |
| 156 | ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) { |
| 157 | return VisitPrePostIncDec(E, false, true); |
| 158 | } |
| 159 | ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) { |
| 160 | return VisitPrePostIncDec(E, true, true); |
| 161 | } |
| 162 | ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 163 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 164 | TestAndClearIgnoreReal(); |
| 165 | TestAndClearIgnoreImag(); |
| 166 | TestAndClearIgnoreRealAssign(); |
| 167 | TestAndClearIgnoreImagAssign(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 168 | return Visit(E->getSubExpr()); |
| 169 | } |
| 170 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 171 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 172 | // LNot,Real,Imag never return complex. |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 173 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 174 | return Visit(E->getSubExpr()); |
| 175 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 176 | ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 177 | return Visit(DAE->getExpr()); |
| 178 | } |
Anders Carlsson | 4b76b4f | 2009-05-31 00:12:05 +0000 | [diff] [blame] | 179 | ComplexPairTy VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { |
| 180 | return CGF.EmitCXXExprWithTemporaries(E).getComplexVal(); |
| 181 | } |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 182 | ComplexPairTy VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { |
| 183 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 184 | QualType Elem = E->getType()->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 185 | llvm::Constant *Null = llvm::Constant::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!"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 190 | QualType Elem = E->getType()->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 191 | llvm::Constant *Null = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 192 | llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 193 | return ComplexPairTy(Null, Null); |
| 194 | } |
Mike Stump | db52dcd | 2009-09-09 13:00: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. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 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); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 211 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 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 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 224 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 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 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 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 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 246 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 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()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +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) { |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 270 | // FIXME: Clean this up once builder takes Twine/StringRef. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 271 | Name += ".realp"; |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 272 | llvm::Value *RealPtr = Builder.CreateStructGEP(SrcPtr, 0, |
| 273 | Name.str().str().c_str()); |
Ted Kremenek | d627889 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 274 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 275 | Name.pop_back(); // .realp -> .real |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 276 | // FIXME: Clean this up once builder takes Twine/StringRef. |
| 277 | Real = Builder.CreateLoad(RealPtr, isVolatile, |
| 278 | Name.str().str().c_str()); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 279 | Name.resize(Name.size()-4); // .real -> .imagp |
| 280 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 281 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 282 | if (!IgnoreImag) { |
| 283 | Name += "imagp"; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 285 | // FIXME: Clean this up once builder takes Twine/StringRef. |
| 286 | llvm::Value *ImagPtr = Builder.CreateStructGEP(SrcPtr, 1, |
| 287 | Name.str().str().c_str()); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 288 | |
| 289 | Name.pop_back(); // .imagp -> .imag |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 290 | // FIXME: Clean this up once builder takes Twine/StringRef. |
| 291 | Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.str().str().c_str()); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 293 | return ComplexPairTy(Real, Imag); |
| 294 | } |
| 295 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 296 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 297 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 298 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 299 | bool isVolatile) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 300 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real"); |
| 301 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 303 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 304 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
| 308 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 309 | //===----------------------------------------------------------------------===// |
| 310 | // Visitor Methods |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 313 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 314 | CGF.ErrorUnsupported(E, "complex expression"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 315 | const llvm::Type *EltTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 316 | CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType()); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 317 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 318 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 321 | ComplexPairTy ComplexExprEmitter:: |
| 322 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 323 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 324 | return |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 325 | ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 329 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 330 | if (E->getCallReturnType()->isReferenceType()) |
| 331 | return EmitLoadOfLValue(E); |
| 332 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 333 | return CGF.EmitCallExpr(E).getComplexVal(); |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 336 | ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 337 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal(); |
| 338 | } |
| 339 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 340 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 341 | ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, |
| 342 | QualType SrcType, |
| 343 | QualType DestType) { |
| 344 | // Get the src/dest element type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 345 | SrcType = SrcType->getAs<ComplexType>()->getElementType(); |
| 346 | DestType = DestType->getAs<ComplexType>()->getElementType(); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 347 | |
Daniel Dunbar | 56f0d16 | 2009-01-29 06:44:03 +0000 | [diff] [blame] | 348 | // 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] | 349 | // complex type, both the real and imaginary parts follow the conversion |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 350 | // rules for the corresponding real types. |
| 351 | Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType); |
| 352 | Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType); |
| 353 | return Val; |
| 354 | } |
| 355 | |
| 356 | ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) { |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 357 | // Two cases here: cast from (complex to complex) and (scalar to complex). |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 358 | if (Op->getType()->isAnyComplexType()) |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 359 | return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 360 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 361 | // 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] | 362 | // 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] | 363 | // conversion to the corresponding real type and the imaginary part of the |
| 364 | // complex result value is a positive zero or an unsigned zero. |
| 365 | llvm::Value *Elt = CGF.EmitScalarExpr(Op); |
| 366 | |
| 367 | // Convert the input element to the element type of the complex. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 368 | DestTy = DestTy->getAs<ComplexType>()->getElementType(); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 369 | Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 370 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 371 | // Return (realval, 0). |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 372 | return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType())); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 375 | ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
| 376 | bool isInc, bool isPre) { |
| 377 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 378 | ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), |
| 379 | LV.isVolatileQualified()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 381 | llvm::Value *NextVal; |
Eli Friedman | c110c54 | 2009-03-23 04:08:46 +0000 | [diff] [blame] | 382 | if (isa<llvm::IntegerType>(InVal.first->getType())) { |
| 383 | uint64_t AmountVal = isInc ? 1 : -1; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 384 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 386 | // Add the inc/dec to the real part. |
| 387 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
Eli Friedman | c110c54 | 2009-03-23 04:08:46 +0000 | [diff] [blame] | 388 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 389 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | c110c54 | 2009-03-23 04:08:46 +0000 | [diff] [blame] | 390 | llvm::APFloat FVal(CGF.getContext().getFloatTypeSemantics(ElemTy), 1); |
| 391 | if (!isInc) |
| 392 | FVal.changeSign(); |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 393 | NextVal = llvm::ConstantFP::get(CGF.getLLVMContext(), FVal); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 395 | // Add the inc/dec to the real part. |
| 396 | NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 397 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 399 | ComplexPairTy IncVal(NextVal, InVal.second); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 401 | // Store the updated result through the lvalue. |
Eli Friedman | c110c54 | 2009-03-23 04:08:46 +0000 | [diff] [blame] | 402 | EmitStoreOfComplex(IncVal, LV.getAddress(), LV.isVolatileQualified()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 404 | // If this is a postinc, return the value read from memory, otherwise use the |
| 405 | // updated value. |
| 406 | return isPre ? IncVal : InVal; |
| 407 | } |
| 408 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 409 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 410 | TestAndClearIgnoreReal(); |
| 411 | TestAndClearIgnoreImag(); |
| 412 | TestAndClearIgnoreRealAssign(); |
| 413 | TestAndClearIgnoreImagAssign(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 414 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 416 | llvm::Value *ResR, *ResI; |
| 417 | if (Op.first->getType()->isFloatingPoint()) { |
| 418 | ResR = Builder.CreateFNeg(Op.first, "neg.r"); |
| 419 | ResI = Builder.CreateFNeg(Op.second, "neg.i"); |
| 420 | } else { |
| 421 | ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 422 | ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 423 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 424 | return ComplexPairTy(ResR, ResI); |
| 425 | } |
| 426 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 427 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 428 | TestAndClearIgnoreReal(); |
| 429 | TestAndClearIgnoreImag(); |
| 430 | TestAndClearIgnoreRealAssign(); |
| 431 | TestAndClearIgnoreImagAssign(); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 432 | // ~(a+ib) = a + i*-b |
| 433 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 434 | llvm::Value *ResI; |
| 435 | if (Op.second->getType()->isFloatingPoint()) |
| 436 | ResI = Builder.CreateFNeg(Op.second, "conj.i"); |
| 437 | else |
| 438 | ResI = Builder.CreateNeg(Op.second, "conj.i"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 439 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 440 | return ComplexPairTy(Op.first, ResI); |
| 441 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 442 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 443 | ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 444 | llvm::Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 446 | if (Op.LHS.first->getType()->isFloatingPoint()) { |
| 447 | ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 448 | ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 449 | } else { |
| 450 | ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 451 | ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 452 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 453 | return ComplexPairTy(ResR, ResI); |
| 454 | } |
| 455 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 456 | ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 457 | llvm::Value *ResR, *ResI; |
| 458 | if (Op.LHS.first->getType()->isFloatingPoint()) { |
| 459 | ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 460 | ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 461 | } else { |
| 462 | ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 463 | ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 464 | } |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 465 | return ComplexPairTy(ResR, ResI); |
| 466 | } |
| 467 | |
| 468 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 469 | ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 470 | using llvm::Value; |
| 471 | Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 473 | if (Op.LHS.first->getType()->isFloatingPoint()) { |
| 474 | Value *ResRl = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 475 | Value *ResRr = Builder.CreateFMul(Op.LHS.second, Op.RHS.second,"mul.rr"); |
| 476 | ResR = Builder.CreateFSub(ResRl, ResRr, "mul.r"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 477 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 478 | Value *ResIl = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 479 | Value *ResIr = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir"); |
| 480 | ResI = Builder.CreateFAdd(ResIl, ResIr, "mul.i"); |
| 481 | } else { |
| 482 | Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 483 | Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr"); |
| 484 | ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
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 | Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 487 | Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir"); |
| 488 | ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
| 489 | } |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 490 | return ComplexPairTy(ResR, ResI); |
| 491 | } |
| 492 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 493 | ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) { |
| 494 | llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second; |
| 495 | llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 497 | |
| 498 | llvm::Value *DSTr, *DSTi; |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 499 | if (Op.LHS.first->getType()->isFloatingPoint()) { |
| 500 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
| 501 | llvm::Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr, "tmp"); // a*c |
| 502 | llvm::Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi, "tmp"); // b*d |
| 503 | llvm::Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2, "tmp"); // ac+bd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 505 | llvm::Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr, "tmp"); // c*c |
| 506 | llvm::Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi, "tmp"); // d*d |
| 507 | llvm::Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5, "tmp"); // cc+dd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 509 | llvm::Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr, "tmp"); // b*c |
| 510 | llvm::Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi, "tmp"); // a*d |
| 511 | llvm::Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8, "tmp"); // bc-ad |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 513 | DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp"); |
| 514 | DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp"); |
| 515 | } else { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 516 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
| 517 | llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c |
| 518 | llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d |
| 519 | llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 521 | llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c |
| 522 | llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d |
| 523 | llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 525 | llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c |
| 526 | llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d |
| 527 | llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 528 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 529 | if (Op.Ty->getAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) { |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 530 | DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp"); |
| 531 | DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp"); |
| 532 | } else { |
| 533 | DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp"); |
| 534 | DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp"); |
| 535 | } |
| 536 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 538 | return ComplexPairTy(DSTr, DSTi); |
| 539 | } |
| 540 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 541 | ComplexExprEmitter::BinOpInfo |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 542 | ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 543 | TestAndClearIgnoreReal(); |
| 544 | TestAndClearIgnoreImag(); |
| 545 | TestAndClearIgnoreRealAssign(); |
| 546 | TestAndClearIgnoreImagAssign(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 547 | BinOpInfo Ops; |
| 548 | Ops.LHS = Visit(E->getLHS()); |
| 549 | Ops.RHS = Visit(E->getRHS()); |
| 550 | Ops.Ty = E->getType(); |
| 551 | return Ops; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | // Compound assignments. |
| 556 | ComplexPairTy ComplexExprEmitter:: |
| 557 | EmitCompoundAssign(const CompoundAssignOperator *E, |
| 558 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 559 | TestAndClearIgnoreReal(); |
| 560 | TestAndClearIgnoreImag(); |
| 561 | bool ignreal = TestAndClearIgnoreRealAssign(); |
| 562 | bool ignimag = TestAndClearIgnoreImagAssign(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 563 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 564 | |
| 565 | BinOpInfo OpInfo; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 566 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 567 | // Load the RHS and LHS operands. |
| 568 | // __block variables need to have the rhs evaluated first, plus this should |
| 569 | // improve codegen a little. It is possible for the RHS to be complex or |
| 570 | // scalar. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 571 | OpInfo.Ty = E->getComputationResultType(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 572 | OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 573 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 574 | LValue LHSLV = CGF.EmitLValue(E->getLHS()); |
| 575 | |
| 576 | |
| 577 | // We know the LHS is a complex lvalue. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 578 | OpInfo.LHS=EmitLoadOfComplex(LHSLV.getAddress(), LHSLV.isVolatileQualified()); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 579 | OpInfo.LHS=EmitComplexToComplexCast(OpInfo.LHS, LHSTy, OpInfo.Ty); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 580 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 581 | // Expand the binary operator. |
| 582 | ComplexPairTy Result = (this->*Func)(OpInfo); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 583 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 584 | // Truncate the result back to the LHS type. |
| 585 | Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 586 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 587 | // Store the result value into the LHS lvalue. |
Eli Friedman | c110c54 | 2009-03-23 04:08:46 +0000 | [diff] [blame] | 588 | EmitStoreOfComplex(Result, LHSLV.getAddress(), LHSLV.isVolatileQualified()); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 589 | // And now return the LHS |
| 590 | IgnoreReal = ignreal; |
| 591 | IgnoreImag = ignimag; |
| 592 | IgnoreRealAssign = ignreal; |
| 593 | IgnoreImagAssign = ignimag; |
| 594 | return EmitLoadOfComplex(LHSLV.getAddress(), LHSLV.isVolatileQualified()); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 595 | } |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 596 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 597 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 598 | TestAndClearIgnoreReal(); |
| 599 | TestAndClearIgnoreImag(); |
| 600 | bool ignreal = TestAndClearIgnoreRealAssign(); |
| 601 | bool ignimag = TestAndClearIgnoreImagAssign(); |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 602 | assert(CGF.getContext().getCanonicalType(E->getLHS()->getType()) == |
| 603 | CGF.getContext().getCanonicalType(E->getRHS()->getType()) && |
| 604 | "Invalid assignment"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 605 | // Emit the RHS. |
| 606 | ComplexPairTy Val = Visit(E->getRHS()); |
| 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 | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 611 | // Store into it, if simple. |
| 612 | if (LHS.isSimple()) { |
| 613 | EmitStoreOfComplex(Val, LHS.getAddress(), LHS.isVolatileQualified()); |
| 614 | |
| 615 | // And now return the LHS |
| 616 | IgnoreReal = ignreal; |
| 617 | IgnoreImag = ignimag; |
| 618 | IgnoreRealAssign = ignreal; |
| 619 | IgnoreImagAssign = ignimag; |
| 620 | return EmitLoadOfComplex(LHS.getAddress(), LHS.isVolatileQualified()); |
| 621 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 622 | |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 623 | // Otherwise we must have a property setter (no complex vector/bitfields). |
| 624 | if (LHS.isPropertyRef()) |
| 625 | CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), RValue::getComplex(Val)); |
| 626 | else |
| 627 | CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), RValue::getComplex(Val)); |
| 628 | |
| 629 | // There is no reload after a store through a method, but we need to restore |
| 630 | // the Ignore* flags. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 631 | IgnoreReal = ignreal; |
| 632 | IgnoreImag = ignimag; |
| 633 | IgnoreRealAssign = ignreal; |
| 634 | IgnoreImagAssign = ignimag; |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 635 | return Val; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 638 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 639 | CGF.EmitStmt(E->getLHS()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 640 | CGF.EnsureInsertPoint(); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 641 | return Visit(E->getRHS()); |
| 642 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 643 | |
| 644 | ComplexPairTy ComplexExprEmitter:: |
| 645 | VisitConditionalOperator(const ConditionalOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 646 | TestAndClearIgnoreReal(); |
| 647 | TestAndClearIgnoreImag(); |
| 648 | TestAndClearIgnoreRealAssign(); |
| 649 | TestAndClearIgnoreImagAssign(); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 650 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 651 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 652 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 653 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 654 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 655 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 656 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 657 | CGF.EmitBlock(LHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 658 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 659 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 660 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 661 | |
| 662 | ComplexPairTy LHS = Visit(E->getLHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 663 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 664 | CGF.EmitBranch(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 665 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 666 | CGF.EmitBlock(RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 667 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 668 | ComplexPairTy RHS = Visit(E->getRHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 669 | RHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 670 | CGF.EmitBranch(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 671 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 672 | CGF.EmitBlock(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 673 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 674 | // Create a PHI node for the real part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 675 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 676 | RealPN->reserveOperandSpace(2); |
| 677 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 678 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 679 | |
| 680 | // Create a PHI node for the imaginary part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 681 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 682 | ImagPN->reserveOperandSpace(2); |
| 683 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 684 | ImagPN->addIncoming(RHS.second, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 685 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 686 | return ComplexPairTy(RealPN, ImagPN); |
| 687 | } |
| 688 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 689 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 690 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 693 | ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 694 | bool Ignore = TestAndClearIgnoreReal(); |
| 695 | (void)Ignore; |
| 696 | assert (Ignore == false && "init list ignored"); |
| 697 | Ignore = TestAndClearIgnoreImag(); |
| 698 | (void)Ignore; |
| 699 | assert (Ignore == false && "init list ignored"); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 700 | if (E->getNumInits()) |
| 701 | return Visit(E->getInit(0)); |
| 702 | |
| 703 | // Empty init list intializes to null |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 704 | QualType Ty = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 705 | const llvm::Type* LTy = CGF.ConvertType(Ty); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 706 | llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 707 | return ComplexPairTy(zeroConstant, zeroConstant); |
| 708 | } |
| 709 | |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 710 | ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 711 | llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 712 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType()); |
| 713 | |
| 714 | if (!ArgPtr) { |
| 715 | CGF.ErrorUnsupported(E, "complex va_arg expression"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 716 | const llvm::Type *EltTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame^] | 717 | CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 718 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 719 | return ComplexPairTy(U, U); |
| 720 | } |
| 721 | |
| 722 | // FIXME Volatility. |
| 723 | return EmitLoadOfComplex(ArgPtr, false); |
| 724 | } |
| 725 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 726 | //===----------------------------------------------------------------------===// |
| 727 | // Entry Point into this File |
| 728 | //===----------------------------------------------------------------------===// |
| 729 | |
| 730 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 731 | /// complex type, ignoring the result. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 732 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal, |
| 733 | bool IgnoreImag, bool IgnoreRealAssign, bool IgnoreImagAssign) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 734 | assert(E && E->getType()->isAnyComplexType() && |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 735 | "Invalid complex expression to emit"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 736 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 737 | return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag, IgnoreRealAssign, |
| 738 | IgnoreImagAssign) |
| 739 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 742 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 743 | /// of complex type, storing into the specified Value*. |
| 744 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 745 | llvm::Value *DestAddr, |
| 746 | bool DestIsVolatile) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 747 | assert(E && E->getType()->isAnyComplexType() && |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 748 | "Invalid complex expression to emit"); |
| 749 | ComplexExprEmitter Emitter(*this); |
| 750 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 751 | Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 752 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 754 | /// StoreComplexToAddr - Store a complex number into the specified address. |
| 755 | void CodeGenFunction::StoreComplexToAddr(ComplexPairTy V, |
| 756 | llvm::Value *DestAddr, |
| 757 | bool DestIsVolatile) { |
| 758 | ComplexExprEmitter(*this).EmitStoreOfComplex(V, DestAddr, DestIsVolatile); |
| 759 | } |
| 760 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 761 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 762 | ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr, |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 763 | bool SrcIsVolatile) { |
| 764 | return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile); |
| 765 | } |