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