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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 16 | #include "clang/AST/AST.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Function.h" |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 25 | // Complex Expression Emitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 28 | typedef CodeGenFunction::ComplexPairTy ComplexPairTy; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | class VISIBILITY_HIDDEN ComplexExprEmitter |
| 32 | : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> { |
| 33 | CodeGenFunction &CGF; |
Devang Patel | 50c9034 | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 34 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 35 | public: |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 36 | ComplexExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
| 40 | //===--------------------------------------------------------------------===// |
| 41 | // Utilities |
| 42 | //===--------------------------------------------------------------------===// |
| 43 | |
| 44 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 45 | /// value l-value, this method emits the address of the l-value, then loads |
| 46 | /// and returns the result. |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 47 | ComplexPairTy EmitLoadOfLValue(const Expr *E) { |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 48 | LValue LV = CGF.EmitLValue(E); |
| 49 | // FIXME: Volatile |
| 50 | return EmitLoadOfComplex(LV.getAddress(), false); |
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) { |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 70 | S->dump(CGF.getContext().SourceMgr); |
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); |
| 118 | // LNot,SizeOf,AlignOf,Real,Imag never return complex. |
| 119 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 120 | return Visit(E->getSubExpr()); |
| 121 | } |
| 122 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 123 | struct BinOpInfo { |
| 124 | ComplexPairTy LHS; |
| 125 | ComplexPairTy RHS; |
| 126 | QualType Ty; // Computation Type. |
| 127 | }; |
| 128 | |
| 129 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
| 130 | ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E, |
| 131 | ComplexPairTy (ComplexExprEmitter::*Func) |
| 132 | (const BinOpInfo &)); |
| 133 | |
| 134 | ComplexPairTy EmitBinAdd(const BinOpInfo &Op); |
| 135 | ComplexPairTy EmitBinSub(const BinOpInfo &Op); |
| 136 | ComplexPairTy EmitBinMul(const BinOpInfo &Op); |
| 137 | ComplexPairTy EmitBinDiv(const BinOpInfo &Op); |
| 138 | |
| 139 | ComplexPairTy VisitBinMul(const BinaryOperator *E) { |
| 140 | return EmitBinMul(EmitBinOps(E)); |
| 141 | } |
| 142 | ComplexPairTy VisitBinAdd(const BinaryOperator *E) { |
| 143 | return EmitBinAdd(EmitBinOps(E)); |
| 144 | } |
| 145 | ComplexPairTy VisitBinSub(const BinaryOperator *E) { |
| 146 | return EmitBinSub(EmitBinOps(E)); |
| 147 | } |
| 148 | ComplexPairTy VisitBinDiv(const BinaryOperator *E) { |
| 149 | return EmitBinDiv(EmitBinOps(E)); |
| 150 | } |
| 151 | |
| 152 | // Compound assignments. |
| 153 | ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) { |
| 154 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd); |
| 155 | } |
| 156 | ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) { |
| 157 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub); |
| 158 | } |
| 159 | ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) { |
| 160 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul); |
| 161 | } |
| 162 | ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) { |
| 163 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv); |
| 164 | } |
| 165 | |
Chris Lattner | 612c40c | 2007-08-26 21:27:07 +0000 | [diff] [blame] | 166 | // GCC rejects rem/and/or/xor for integer complex. |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 167 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 168 | |
| 169 | // No comparisons produce a complex result. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 170 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 171 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 172 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 173 | |
| 174 | ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 175 | ComplexPairTy VisitChooseExpr(ChooseExpr *CE); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 176 | }; |
| 177 | } // end anonymous namespace. |
| 178 | |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | // Utilities |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 183 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 184 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 185 | ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr, |
| 186 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 187 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 188 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 190 | llvm::SmallString<64> Name(SrcPtr->getNameStart(), |
| 191 | SrcPtr->getNameStart()+SrcPtr->getNameLen()); |
| 192 | |
| 193 | Name += ".realp"; |
Ted Kremenek | d627889 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 194 | llvm::Value *Ops[] = {Zero, Zero}; |
| 195 | llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Ops, Ops+2, Name.c_str()); |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 196 | |
| 197 | Name.pop_back(); // .realp -> .real |
| 198 | llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, Name.c_str()); |
| 199 | |
| 200 | Name.resize(Name.size()-4); // .real -> .imagp |
| 201 | Name += "imagp"; |
Ted Kremenek | d627889 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 202 | |
| 203 | Ops[1] = One; // { Ops = { Zero, One } |
| 204 | llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Ops, Ops+2, Name.c_str()); |
| 205 | |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 206 | Name.pop_back(); // .imagp -> .imag |
| 207 | llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.c_str()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 208 | return ComplexPairTy(Real, Imag); |
| 209 | } |
| 210 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 211 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 212 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 213 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 214 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 215 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 216 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
Ted Kremenek | d627889 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 217 | |
| 218 | llvm::Value *Ops[] = {Zero, Zero}; |
| 219 | llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Ops, Ops+2, "real"); |
| 220 | |
| 221 | Ops[1] = One; // { Ops = { Zero, One } |
| 222 | llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Ops, Ops+2, "imag"); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 224 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 225 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 230 | //===----------------------------------------------------------------------===// |
| 231 | // Visitor Methods |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 234 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame^] | 235 | CGF.WarnUnsupported(E, "complex expression"); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 236 | const llvm::Type *EltTy = |
| 237 | CGF.ConvertType(E->getType()->getAsComplexType()->getElementType()); |
| 238 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 239 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 242 | ComplexPairTy ComplexExprEmitter:: |
| 243 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 244 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
| 245 | return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
| 246 | } |
| 247 | |
| 248 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 249 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 250 | return CGF.EmitCallExpr(E).getComplexVal(); |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 253 | ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 254 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal(); |
| 255 | } |
| 256 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 257 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 258 | ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, |
| 259 | QualType SrcType, |
| 260 | QualType DestType) { |
| 261 | // Get the src/dest element type. |
| 262 | SrcType = cast<ComplexType>(SrcType.getCanonicalType())->getElementType(); |
| 263 | DestType = cast<ComplexType>(DestType.getCanonicalType())->getElementType(); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 264 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 265 | // C99 6.3.1.6: When a value of complextype is converted to another |
| 266 | // complex type, both the real and imaginary parts followthe conversion |
| 267 | // rules for the corresponding real types. |
| 268 | Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType); |
| 269 | Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType); |
| 270 | return Val; |
| 271 | } |
| 272 | |
| 273 | ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) { |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 274 | // Two cases here: cast from (complex to complex) and (scalar to complex). |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 275 | if (Op->getType()->isComplexType()) |
| 276 | return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy); |
| 277 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 278 | // C99 6.3.1.7: When a value of real type is converted to a complex type, the |
| 279 | // real part of the complex result value is determined by the rules of |
| 280 | // conversion to the corresponding real type and the imaginary part of the |
| 281 | // complex result value is a positive zero or an unsigned zero. |
| 282 | llvm::Value *Elt = CGF.EmitScalarExpr(Op); |
| 283 | |
| 284 | // Convert the input element to the element type of the complex. |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 285 | DestTy = cast<ComplexType>(DestTy.getCanonicalType())->getElementType(); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 286 | Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 287 | |
| 288 | // Return (realval, 0). |
| 289 | return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType())); |
| 290 | } |
| 291 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 292 | ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
| 293 | bool isInc, bool isPre) { |
| 294 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 295 | // FIXME: Handle volatile! |
| 296 | ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), false); |
| 297 | |
Hartmut Kaiser | 124dd7b | 2007-09-07 14:34:20 +0000 | [diff] [blame] | 298 | uint64_t AmountVal = isInc ? 1 : -1; |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 299 | |
| 300 | llvm::Value *NextVal; |
| 301 | if (isa<llvm::IntegerType>(InVal.first->getType())) |
| 302 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 303 | else if (InVal.first->getType() == llvm::Type::FloatTy) |
| 304 | // FIXME: Handle long double. |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 305 | NextVal = |
| 306 | llvm::ConstantFP::get(InVal.first->getType(), |
| 307 | llvm::APFloat(static_cast<float>(AmountVal))); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 308 | else { |
| 309 | // FIXME: Handle long double. |
| 310 | assert(InVal.first->getType() == llvm::Type::DoubleTy); |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 311 | NextVal = |
| 312 | llvm::ConstantFP::get(InVal.first->getType(), |
| 313 | llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 315 | |
| 316 | // Add the inc/dec to the real part. |
| 317 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 318 | |
| 319 | ComplexPairTy IncVal(NextVal, InVal.second); |
| 320 | |
| 321 | // Store the updated result through the lvalue. |
| 322 | EmitStoreOfComplex(IncVal, LV.getAddress(), false); /* FIXME: Volatile */ |
| 323 | |
| 324 | // If this is a postinc, return the value read from memory, otherwise use the |
| 325 | // updated value. |
| 326 | return isPre ? IncVal : InVal; |
| 327 | } |
| 328 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 329 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 330 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 331 | llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 332 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 333 | return ComplexPairTy(ResR, ResI); |
| 334 | } |
| 335 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 336 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 337 | // ~(a+ib) = a + i*-b |
| 338 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 339 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i"); |
| 340 | return ComplexPairTy(Op.first, ResI); |
| 341 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 342 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 343 | ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { |
| 344 | llvm::Value *ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 345 | 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] | 346 | return ComplexPairTy(ResR, ResI); |
| 347 | } |
| 348 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 349 | ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { |
| 350 | llvm::Value *ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 351 | 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] | 352 | return ComplexPairTy(ResR, ResI); |
| 353 | } |
| 354 | |
| 355 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 356 | ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { |
| 357 | llvm::Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 358 | 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] | 359 | llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 360 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 361 | llvm::Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 362 | 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] | 363 | llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 364 | return ComplexPairTy(ResR, ResI); |
| 365 | } |
| 366 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 367 | ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) { |
| 368 | llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second; |
| 369 | llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second; |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 370 | |
| 371 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
| 372 | llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c |
| 373 | llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d |
| 374 | llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd |
| 375 | |
| 376 | llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c |
| 377 | llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d |
| 378 | llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd |
| 379 | |
| 380 | llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c |
| 381 | llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d |
| 382 | llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad |
| 383 | |
| 384 | llvm::Value *DSTr, *DSTi; |
| 385 | if (Tmp3->getType()->isFloatingPoint()) { |
| 386 | DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp"); |
| 387 | DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp"); |
| 388 | } else { |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 389 | if (Op.Ty->getAsComplexType()->getElementType()->isUnsignedIntegerType()) { |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 390 | DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp"); |
| 391 | DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp"); |
| 392 | } else { |
| 393 | DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp"); |
| 394 | DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp"); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return ComplexPairTy(DSTr, DSTi); |
| 399 | } |
| 400 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 401 | ComplexExprEmitter::BinOpInfo |
| 402 | ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 403 | BinOpInfo Ops; |
| 404 | Ops.LHS = Visit(E->getLHS()); |
| 405 | Ops.RHS = Visit(E->getRHS()); |
| 406 | Ops.Ty = E->getType(); |
| 407 | return Ops; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | // Compound assignments. |
| 412 | ComplexPairTy ComplexExprEmitter:: |
| 413 | EmitCompoundAssign(const CompoundAssignOperator *E, |
| 414 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ |
| 415 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 416 | |
| 417 | // Load the LHS and RHS operands. |
| 418 | LValue LHSLV = CGF.EmitLValue(E->getLHS()); |
| 419 | |
| 420 | BinOpInfo OpInfo; |
| 421 | OpInfo.Ty = E->getComputationType(); |
| 422 | |
| 423 | // We know the LHS is a complex lvalue. |
| 424 | OpInfo.LHS = EmitLoadOfComplex(LHSLV.getAddress(), false);// FIXME: Volatile. |
| 425 | OpInfo.LHS = EmitComplexToComplexCast(OpInfo.LHS, LHSTy, OpInfo.Ty); |
| 426 | |
| 427 | // It is possible for the RHS to be complex or scalar. |
| 428 | OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty); |
| 429 | |
| 430 | // Expand the binary operator. |
| 431 | ComplexPairTy Result = (this->*Func)(OpInfo); |
| 432 | |
| 433 | // Truncate the result back to the LHS type. |
| 434 | Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy); |
| 435 | |
| 436 | // Store the result value into the LHS lvalue. |
| 437 | EmitStoreOfComplex(Result, LHSLV.getAddress(), false); // FIXME: VOLATILE |
| 438 | return Result; |
| 439 | } |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 440 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 441 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 442 | assert(E->getLHS()->getType().getCanonicalType() == |
| 443 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
| 444 | // Emit the RHS. |
| 445 | ComplexPairTy Val = Visit(E->getRHS()); |
| 446 | |
| 447 | // Compute the address to store into. |
| 448 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
| 449 | |
| 450 | // Store into it. |
| 451 | // FIXME: Volatility! |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 452 | EmitStoreOfComplex(Val, LHS.getAddress(), false); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 453 | return Val; |
| 454 | } |
| 455 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 456 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 457 | CGF.EmitStmt(E->getLHS()); |
| 458 | return Visit(E->getRHS()); |
| 459 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 460 | |
| 461 | ComplexPairTy ComplexExprEmitter:: |
| 462 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 463 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 464 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 465 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 466 | |
| 467 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 468 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 469 | |
| 470 | CGF.EmitBlock(LHSBlock); |
| 471 | |
| 472 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 473 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 474 | |
| 475 | ComplexPairTy LHS = Visit(E->getLHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 476 | Builder.CreateBr(ContBlock); |
| 477 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 478 | |
| 479 | CGF.EmitBlock(RHSBlock); |
| 480 | |
| 481 | ComplexPairTy RHS = Visit(E->getRHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 482 | Builder.CreateBr(ContBlock); |
| 483 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 484 | |
| 485 | CGF.EmitBlock(ContBlock); |
| 486 | |
| 487 | // Create a PHI node for the real part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 488 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 489 | RealPN->reserveOperandSpace(2); |
| 490 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 491 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 492 | |
| 493 | // Create a PHI node for the imaginary part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 494 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 495 | ImagPN->reserveOperandSpace(2); |
| 496 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 497 | ImagPN->addIncoming(RHS.second, RHSBlock); |
| 498 | |
| 499 | return ComplexPairTy(RealPN, ImagPN); |
| 500 | } |
| 501 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 502 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 503 | // Emit the LHS or RHS as appropriate. |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 504 | return Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() :E->getRHS()); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 507 | //===----------------------------------------------------------------------===// |
| 508 | // Entry Point into this File |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | |
| 511 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 512 | /// complex type, ignoring the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 513 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 514 | assert(E && E->getType()->isComplexType() && |
| 515 | "Invalid complex expression to emit"); |
| 516 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 517 | return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 520 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 521 | /// of complex type, storing into the specified Value*. |
| 522 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 523 | llvm::Value *DestAddr, |
| 524 | bool DestIsVolatile) { |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 525 | assert(E && E->getType()->isComplexType() && |
| 526 | "Invalid complex expression to emit"); |
| 527 | ComplexExprEmitter Emitter(*this); |
| 528 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 529 | Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 530 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 531 | |
| 532 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
| 533 | ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr, |
| 534 | bool SrcIsVolatile) { |
| 535 | return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile); |
| 536 | } |