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; |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 34 | llvm::LLVMBuilder &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 | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 70 | S->dump(); |
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"; |
| 194 | llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Zero, Zero, Name.c_str()); |
| 195 | |
| 196 | Name.pop_back(); // .realp -> .real |
| 197 | llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, Name.c_str()); |
| 198 | |
| 199 | Name.resize(Name.size()-4); // .real -> .imagp |
| 200 | Name += "imagp"; |
| 201 | llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Zero, One, Name.c_str()); |
| 202 | Name.pop_back(); // .imagp -> .imag |
| 203 | llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.c_str()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 204 | return ComplexPairTy(Real, Imag); |
| 205 | } |
| 206 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 207 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 208 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 209 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 210 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 211 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 212 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 213 | llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Zero, Zero, "real"); |
| 214 | llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Zero, One, "imag"); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 216 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 217 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 222 | //===----------------------------------------------------------------------===// |
| 223 | // Visitor Methods |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 226 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
| 227 | fprintf(stderr, "Unimplemented complex expr!\n"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 228 | E->dump(); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 229 | const llvm::Type *EltTy = |
| 230 | CGF.ConvertType(E->getType()->getAsComplexType()->getElementType()); |
| 231 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 232 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 235 | ComplexPairTy ComplexExprEmitter:: |
| 236 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 237 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
| 238 | return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
| 239 | } |
| 240 | |
| 241 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 242 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 243 | return CGF.EmitCallExpr(E).getComplexVal(); |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame^] | 246 | ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 247 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal(); |
| 248 | } |
| 249 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 250 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 251 | ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, |
| 252 | QualType SrcType, |
| 253 | QualType DestType) { |
| 254 | // Get the src/dest element type. |
| 255 | SrcType = cast<ComplexType>(SrcType.getCanonicalType())->getElementType(); |
| 256 | DestType = cast<ComplexType>(DestType.getCanonicalType())->getElementType(); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 257 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 258 | // C99 6.3.1.6: When a value of complextype is converted to another |
| 259 | // complex type, both the real and imaginary parts followthe conversion |
| 260 | // rules for the corresponding real types. |
| 261 | Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType); |
| 262 | Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType); |
| 263 | return Val; |
| 264 | } |
| 265 | |
| 266 | ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) { |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 267 | // Two cases here: cast from (complex to complex) and (scalar to complex). |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 268 | if (Op->getType()->isComplexType()) |
| 269 | return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy); |
| 270 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 271 | // C99 6.3.1.7: When a value of real type is converted to a complex type, the |
| 272 | // real part of the complex result value is determined by the rules of |
| 273 | // conversion to the corresponding real type and the imaginary part of the |
| 274 | // complex result value is a positive zero or an unsigned zero. |
| 275 | llvm::Value *Elt = CGF.EmitScalarExpr(Op); |
| 276 | |
| 277 | // Convert the input element to the element type of the complex. |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 278 | DestTy = cast<ComplexType>(DestTy.getCanonicalType())->getElementType(); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 279 | Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 280 | |
| 281 | // Return (realval, 0). |
| 282 | return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType())); |
| 283 | } |
| 284 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 285 | ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
| 286 | bool isInc, bool isPre) { |
| 287 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 288 | // FIXME: Handle volatile! |
| 289 | ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), false); |
| 290 | |
| 291 | int AmountVal = isInc ? 1 : -1; |
| 292 | |
| 293 | llvm::Value *NextVal; |
| 294 | if (isa<llvm::IntegerType>(InVal.first->getType())) |
| 295 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal); |
| 296 | else |
| 297 | NextVal = llvm::ConstantFP::get(InVal.first->getType(), AmountVal); |
| 298 | |
| 299 | // Add the inc/dec to the real part. |
| 300 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 301 | |
| 302 | ComplexPairTy IncVal(NextVal, InVal.second); |
| 303 | |
| 304 | // Store the updated result through the lvalue. |
| 305 | EmitStoreOfComplex(IncVal, LV.getAddress(), false); /* FIXME: Volatile */ |
| 306 | |
| 307 | // If this is a postinc, return the value read from memory, otherwise use the |
| 308 | // updated value. |
| 309 | return isPre ? IncVal : InVal; |
| 310 | } |
| 311 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 312 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 313 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 314 | llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 315 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 316 | return ComplexPairTy(ResR, ResI); |
| 317 | } |
| 318 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 319 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 320 | // ~(a+ib) = a + i*-b |
| 321 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 322 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i"); |
| 323 | return ComplexPairTy(Op.first, ResI); |
| 324 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 325 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 326 | ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { |
| 327 | llvm::Value *ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 328 | 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] | 329 | return ComplexPairTy(ResR, ResI); |
| 330 | } |
| 331 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 332 | ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { |
| 333 | llvm::Value *ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 334 | 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] | 335 | return ComplexPairTy(ResR, ResI); |
| 336 | } |
| 337 | |
| 338 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 339 | ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { |
| 340 | llvm::Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 341 | 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] | 342 | llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 343 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 344 | llvm::Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 345 | 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] | 346 | llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 347 | return ComplexPairTy(ResR, ResI); |
| 348 | } |
| 349 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 350 | ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) { |
| 351 | llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second; |
| 352 | llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second; |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 353 | |
| 354 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
| 355 | llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c |
| 356 | llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d |
| 357 | llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd |
| 358 | |
| 359 | llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c |
| 360 | llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d |
| 361 | llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd |
| 362 | |
| 363 | llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c |
| 364 | llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d |
| 365 | llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad |
| 366 | |
| 367 | llvm::Value *DSTr, *DSTi; |
| 368 | if (Tmp3->getType()->isFloatingPoint()) { |
| 369 | DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp"); |
| 370 | DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp"); |
| 371 | } else { |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 372 | if (Op.Ty->getAsComplexType()->getElementType()->isUnsignedIntegerType()) { |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 373 | DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp"); |
| 374 | DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp"); |
| 375 | } else { |
| 376 | DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp"); |
| 377 | DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp"); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return ComplexPairTy(DSTr, DSTi); |
| 382 | } |
| 383 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 384 | ComplexExprEmitter::BinOpInfo |
| 385 | ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 386 | BinOpInfo Ops; |
| 387 | Ops.LHS = Visit(E->getLHS()); |
| 388 | Ops.RHS = Visit(E->getRHS()); |
| 389 | Ops.Ty = E->getType(); |
| 390 | return Ops; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | // Compound assignments. |
| 395 | ComplexPairTy ComplexExprEmitter:: |
| 396 | EmitCompoundAssign(const CompoundAssignOperator *E, |
| 397 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ |
| 398 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 399 | |
| 400 | // Load the LHS and RHS operands. |
| 401 | LValue LHSLV = CGF.EmitLValue(E->getLHS()); |
| 402 | |
| 403 | BinOpInfo OpInfo; |
| 404 | OpInfo.Ty = E->getComputationType(); |
| 405 | |
| 406 | // We know the LHS is a complex lvalue. |
| 407 | OpInfo.LHS = EmitLoadOfComplex(LHSLV.getAddress(), false);// FIXME: Volatile. |
| 408 | OpInfo.LHS = EmitComplexToComplexCast(OpInfo.LHS, LHSTy, OpInfo.Ty); |
| 409 | |
| 410 | // It is possible for the RHS to be complex or scalar. |
| 411 | OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty); |
| 412 | |
| 413 | // Expand the binary operator. |
| 414 | ComplexPairTy Result = (this->*Func)(OpInfo); |
| 415 | |
| 416 | // Truncate the result back to the LHS type. |
| 417 | Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy); |
| 418 | |
| 419 | // Store the result value into the LHS lvalue. |
| 420 | EmitStoreOfComplex(Result, LHSLV.getAddress(), false); // FIXME: VOLATILE |
| 421 | return Result; |
| 422 | } |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 423 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 424 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 425 | assert(E->getLHS()->getType().getCanonicalType() == |
| 426 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
| 427 | // Emit the RHS. |
| 428 | ComplexPairTy Val = Visit(E->getRHS()); |
| 429 | |
| 430 | // Compute the address to store into. |
| 431 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
| 432 | |
| 433 | // Store into it. |
| 434 | // FIXME: Volatility! |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 435 | EmitStoreOfComplex(Val, LHS.getAddress(), false); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 436 | return Val; |
| 437 | } |
| 438 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 439 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 440 | CGF.EmitStmt(E->getLHS()); |
| 441 | return Visit(E->getRHS()); |
| 442 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 443 | |
| 444 | ComplexPairTy ComplexExprEmitter:: |
| 445 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 446 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 447 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 448 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 449 | |
| 450 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 451 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 452 | |
| 453 | CGF.EmitBlock(LHSBlock); |
| 454 | |
| 455 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 456 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 457 | |
| 458 | ComplexPairTy LHS = Visit(E->getLHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 459 | Builder.CreateBr(ContBlock); |
| 460 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 461 | |
| 462 | CGF.EmitBlock(RHSBlock); |
| 463 | |
| 464 | ComplexPairTy RHS = Visit(E->getRHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 465 | Builder.CreateBr(ContBlock); |
| 466 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 467 | |
| 468 | CGF.EmitBlock(ContBlock); |
| 469 | |
| 470 | // Create a PHI node for the real part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 471 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 472 | RealPN->reserveOperandSpace(2); |
| 473 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 474 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 475 | |
| 476 | // Create a PHI node for the imaginary part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 477 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 478 | ImagPN->reserveOperandSpace(2); |
| 479 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 480 | ImagPN->addIncoming(RHS.second, RHSBlock); |
| 481 | |
| 482 | return ComplexPairTy(RealPN, ImagPN); |
| 483 | } |
| 484 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 485 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
| 486 | llvm::APSInt CondVal(32); |
| 487 | bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext()); |
| 488 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 489 | |
| 490 | // Emit the LHS or RHS as appropriate. |
| 491 | return Visit(CondVal != 0 ? E->getLHS() : E->getRHS()); |
| 492 | } |
| 493 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 494 | //===----------------------------------------------------------------------===// |
| 495 | // Entry Point into this File |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | |
| 498 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 499 | /// complex type, ignoring the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 500 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 501 | assert(E && E->getType()->isComplexType() && |
| 502 | "Invalid complex expression to emit"); |
| 503 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 504 | return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 507 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 508 | /// of complex type, storing into the specified Value*. |
| 509 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 510 | llvm::Value *DestAddr, |
| 511 | bool DestIsVolatile) { |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 512 | assert(E && E->getType()->isComplexType() && |
| 513 | "Invalid complex expression to emit"); |
| 514 | ComplexExprEmitter Emitter(*this); |
| 515 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 516 | Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 517 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 518 | |
| 519 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
| 520 | ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr, |
| 521 | bool SrcIsVolatile) { |
| 522 | return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile); |
| 523 | } |