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