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