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