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" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 24 | // Complex Expression Emitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 27 | typedef CodeGenFunction::ComplexPairTy ComplexPairTy; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | class VISIBILITY_HIDDEN ComplexExprEmitter |
| 31 | : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> { |
| 32 | CodeGenFunction &CGF; |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 33 | llvm::LLVMBuilder &Builder; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 34 | public: |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 35 | ComplexExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
| 39 | //===--------------------------------------------------------------------===// |
| 40 | // Utilities |
| 41 | //===--------------------------------------------------------------------===// |
| 42 | |
| 43 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 44 | /// value l-value, this method emits the address of the l-value, then loads |
| 45 | /// and returns the result. |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 46 | ComplexPairTy EmitLoadOfLValue(const Expr *E) { |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 47 | LValue LV = CGF.EmitLValue(E); |
| 48 | // FIXME: Volatile |
| 49 | return EmitLoadOfComplex(LV.getAddress(), false); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 50 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 52 | /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load |
| 53 | /// the real and imaginary pieces. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 54 | ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 55 | |
| 56 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 57 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 58 | void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 59 | |
| 60 | //===--------------------------------------------------------------------===// |
| 61 | // Visitor Methods |
| 62 | //===--------------------------------------------------------------------===// |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 63 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 64 | ComplexPairTy VisitStmt(Stmt *S) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 65 | S->dump(); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 66 | assert(0 && "Stmt can't have complex result type!"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 67 | return ComplexPairTy(); |
| 68 | } |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 69 | ComplexPairTy VisitExpr(Expr *S); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 70 | ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());} |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 71 | ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL); |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 72 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 73 | // l-values. |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 74 | ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | 8bcd723 | 2007-08-21 18:03:58 +0000 | [diff] [blame] | 75 | ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 76 | ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 77 | |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 78 | // FIXME: CompoundLiteralExpr |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 79 | |
| 80 | ComplexPairTy EmitCast(Expr *Op, QualType DestTy); |
| 81 | ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 82 | // Unlike for scalars, we don't have to worry about function->ptr demotion |
| 83 | // here. |
| 84 | return EmitCast(E->getSubExpr(), E->getType()); |
| 85 | } |
| 86 | ComplexPairTy VisitCastExpr(CastExpr *E) { |
| 87 | return EmitCast(E->getSubExpr(), E->getType()); |
| 88 | } |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 89 | ComplexPairTy VisitCallExpr(const CallExpr *E); |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 90 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 91 | // Operators. |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 92 | ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E, |
| 93 | bool isInc, bool isPre); |
| 94 | ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) { |
| 95 | return VisitPrePostIncDec(E, false, false); |
| 96 | } |
| 97 | ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) { |
| 98 | return VisitPrePostIncDec(E, true, false); |
| 99 | } |
| 100 | ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) { |
| 101 | return VisitPrePostIncDec(E, false, true); |
| 102 | } |
| 103 | ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) { |
| 104 | return VisitPrePostIncDec(E, true, true); |
| 105 | } |
| 106 | ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 107 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
| 108 | return Visit(E->getSubExpr()); |
| 109 | } |
| 110 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 111 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
| 112 | // LNot,SizeOf,AlignOf,Real,Imag never return complex. |
| 113 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 114 | return Visit(E->getSubExpr()); |
| 115 | } |
| 116 | |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 117 | ComplexPairTy VisitBinMul (const BinaryOperator *E); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 118 | ComplexPairTy VisitBinAdd (const BinaryOperator *E); |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 119 | ComplexPairTy VisitBinSub (const BinaryOperator *E); |
| 120 | // FIXME: div/rem |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 121 | // GCC rejects and/or/xor for integer complex. |
| 122 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 123 | |
| 124 | // No comparisons produce a complex result. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 125 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 126 | // FIXME: Compound assignment operators. |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 127 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 128 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 129 | |
| 130 | ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 131 | ComplexPairTy VisitChooseExpr(ChooseExpr *CE); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 132 | }; |
| 133 | } // end anonymous namespace. |
| 134 | |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | // Utilities |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 139 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 140 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 141 | ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr, |
| 142 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 143 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 144 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 145 | // FIXME: It would be nice to make this "Ptr->getName()+realp" |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 146 | llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Zero, Zero, "realp"); |
| 147 | llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Zero, One, "imagp"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 148 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 149 | // FIXME: It would be nice to make this "Ptr->getName()+real" |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 150 | llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, "real"); |
| 151 | llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, "imag"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 152 | return ComplexPairTy(Real, Imag); |
| 153 | } |
| 154 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 155 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 156 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 157 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 158 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 159 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 160 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 161 | llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Zero, Zero, "real"); |
| 162 | llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Zero, One, "imag"); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 164 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 165 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // Visitor Methods |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 174 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
| 175 | fprintf(stderr, "Unimplemented complex expr!\n"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 176 | E->dump(); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 177 | const llvm::Type *EltTy = |
| 178 | CGF.ConvertType(E->getType()->getAsComplexType()->getElementType()); |
| 179 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 180 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 183 | ComplexPairTy ComplexExprEmitter:: |
| 184 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 185 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
| 186 | return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
| 187 | } |
| 188 | |
| 189 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 190 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
| 191 | llvm::Value *AggPtr = CGF.EmitCallExpr(E).getAggregateAddr(); |
| 192 | return EmitLoadOfComplex(AggPtr, false); |
| 193 | } |
| 194 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 195 | ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) { |
| 196 | // Get the destination element type. |
| 197 | DestTy = cast<ComplexType>(DestTy.getCanonicalType())->getElementType(); |
| 198 | |
| 199 | // Two cases here: cast from (complex to complex) and (scalar to complex). |
| 200 | if (const ComplexType *CT = Op->getType()->getAsComplexType()) { |
| 201 | // C99 6.3.1.6: When a value of complextype is converted to another |
| 202 | // complex type, both the real and imaginary parts followthe conversion |
| 203 | // rules for the corresponding real types. |
| 204 | ComplexPairTy Res = Visit(Op); |
| 205 | QualType SrcEltTy = CT->getElementType(); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 206 | Res.first = CGF.EmitScalarConversion(Res.first, SrcEltTy, DestTy); |
| 207 | Res.second = CGF.EmitScalarConversion(Res.second, SrcEltTy, DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 208 | return Res; |
| 209 | } |
| 210 | // C99 6.3.1.7: When a value of real type is converted to a complex type, the |
| 211 | // real part of the complex result value is determined by the rules of |
| 212 | // conversion to the corresponding real type and the imaginary part of the |
| 213 | // complex result value is a positive zero or an unsigned zero. |
| 214 | llvm::Value *Elt = CGF.EmitScalarExpr(Op); |
| 215 | |
| 216 | // Convert the input element to the element type of the complex. |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 217 | Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 218 | |
| 219 | // Return (realval, 0). |
| 220 | return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType())); |
| 221 | } |
| 222 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 223 | ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
| 224 | bool isInc, bool isPre) { |
| 225 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 226 | // FIXME: Handle volatile! |
| 227 | ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), false); |
| 228 | |
| 229 | int AmountVal = isInc ? 1 : -1; |
| 230 | |
| 231 | llvm::Value *NextVal; |
| 232 | if (isa<llvm::IntegerType>(InVal.first->getType())) |
| 233 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal); |
| 234 | else |
| 235 | NextVal = llvm::ConstantFP::get(InVal.first->getType(), AmountVal); |
| 236 | |
| 237 | // Add the inc/dec to the real part. |
| 238 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 239 | |
| 240 | ComplexPairTy IncVal(NextVal, InVal.second); |
| 241 | |
| 242 | // Store the updated result through the lvalue. |
| 243 | EmitStoreOfComplex(IncVal, LV.getAddress(), false); /* FIXME: Volatile */ |
| 244 | |
| 245 | // If this is a postinc, return the value read from memory, otherwise use the |
| 246 | // updated value. |
| 247 | return isPre ? IncVal : InVal; |
| 248 | } |
| 249 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 250 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 251 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 252 | llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 253 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 254 | return ComplexPairTy(ResR, ResI); |
| 255 | } |
| 256 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 257 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 258 | // ~(a+ib) = a + i*-b |
| 259 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 260 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i"); |
| 261 | return ComplexPairTy(Op.first, ResI); |
| 262 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 263 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 264 | ComplexPairTy ComplexExprEmitter::VisitBinAdd(const BinaryOperator *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 265 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 266 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 267 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 268 | llvm::Value *ResR = Builder.CreateAdd(LHS.first, RHS.first, "add.r"); |
| 269 | llvm::Value *ResI = Builder.CreateAdd(LHS.second, RHS.second, "add.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 270 | |
| 271 | return ComplexPairTy(ResR, ResI); |
| 272 | } |
| 273 | |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 274 | ComplexPairTy ComplexExprEmitter::VisitBinSub(const BinaryOperator *E) { |
| 275 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 276 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 277 | |
| 278 | llvm::Value *ResR = Builder.CreateSub(LHS.first, RHS.first, "sub.r"); |
| 279 | llvm::Value *ResI = Builder.CreateSub(LHS.second, RHS.second, "sub.i"); |
| 280 | |
| 281 | return ComplexPairTy(ResR, ResI); |
| 282 | } |
| 283 | |
| 284 | |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 285 | ComplexPairTy ComplexExprEmitter::VisitBinMul(const BinaryOperator *E) { |
| 286 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 287 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 288 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 289 | llvm::Value *ResRl = Builder.CreateMul(LHS.first, RHS.first, "mul.rl"); |
| 290 | llvm::Value *ResRr = Builder.CreateMul(LHS.second, RHS.second, "mul.rr"); |
| 291 | llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 293 | llvm::Value *ResIl = Builder.CreateMul(LHS.second, RHS.first, "mul.il"); |
| 294 | llvm::Value *ResIr = Builder.CreateMul(LHS.first, RHS.second, "mul.ir"); |
| 295 | llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 296 | |
| 297 | return ComplexPairTy(ResR, ResI); |
| 298 | } |
| 299 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 300 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 301 | assert(E->getLHS()->getType().getCanonicalType() == |
| 302 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
| 303 | // Emit the RHS. |
| 304 | ComplexPairTy Val = Visit(E->getRHS()); |
| 305 | |
| 306 | // Compute the address to store into. |
| 307 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
| 308 | |
| 309 | // Store into it. |
| 310 | // FIXME: Volatility! |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 311 | EmitStoreOfComplex(Val, LHS.getAddress(), false); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 312 | return Val; |
| 313 | } |
| 314 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 315 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 316 | CGF.EmitStmt(E->getLHS()); |
| 317 | return Visit(E->getRHS()); |
| 318 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 319 | |
| 320 | ComplexPairTy ComplexExprEmitter:: |
| 321 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 322 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 323 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 324 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 325 | |
| 326 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 327 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 328 | |
| 329 | CGF.EmitBlock(LHSBlock); |
| 330 | |
| 331 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 332 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 333 | |
| 334 | ComplexPairTy LHS = Visit(E->getLHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 335 | Builder.CreateBr(ContBlock); |
| 336 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 337 | |
| 338 | CGF.EmitBlock(RHSBlock); |
| 339 | |
| 340 | ComplexPairTy RHS = Visit(E->getRHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 341 | Builder.CreateBr(ContBlock); |
| 342 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 343 | |
| 344 | CGF.EmitBlock(ContBlock); |
| 345 | |
| 346 | // Create a PHI node for the real part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 347 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 348 | RealPN->reserveOperandSpace(2); |
| 349 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 350 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 351 | |
| 352 | // Create a PHI node for the imaginary part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 353 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 354 | ImagPN->reserveOperandSpace(2); |
| 355 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 356 | ImagPN->addIncoming(RHS.second, RHSBlock); |
| 357 | |
| 358 | return ComplexPairTy(RealPN, ImagPN); |
| 359 | } |
| 360 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 361 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
| 362 | llvm::APSInt CondVal(32); |
| 363 | bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext()); |
| 364 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 365 | |
| 366 | // Emit the LHS or RHS as appropriate. |
| 367 | return Visit(CondVal != 0 ? E->getLHS() : E->getRHS()); |
| 368 | } |
| 369 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 370 | //===----------------------------------------------------------------------===// |
| 371 | // Entry Point into this File |
| 372 | //===----------------------------------------------------------------------===// |
| 373 | |
| 374 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 375 | /// complex type, ignoring the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 376 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 377 | assert(E && E->getType()->isComplexType() && |
| 378 | "Invalid complex expression to emit"); |
| 379 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 380 | return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 383 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 384 | /// of complex type, storing into the specified Value*. |
| 385 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame^] | 386 | llvm::Value *DestAddr, |
| 387 | bool DestIsVolatile) { |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 388 | assert(E && E->getType()->isComplexType() && |
| 389 | "Invalid complex expression to emit"); |
| 390 | ComplexExprEmitter Emitter(*this); |
| 391 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame^] | 392 | Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 393 | } |