Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 1 | //===--- CGComplexExpr.cpp - Emit LLVM Code for Complex Exprs -------------===// |
| 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());} |
| 71 | |
| 72 | // l-values. |
Chris Lattner | 8bcd723 | 2007-08-21 18:03:58 +0000 | [diff] [blame] | 73 | ComplexPairTy VisitDeclRefExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 74 | ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 75 | ComplexPairTy VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 76 | |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 77 | // FIXME: CompoundLiteralExpr |
| 78 | // FIXME: ImplicitCastExpr |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 79 | ComplexPairTy VisitCallExpr(const CallExpr *E); |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 80 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 81 | // Operators. |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 82 | ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E, |
| 83 | bool isInc, bool isPre); |
| 84 | ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) { |
| 85 | return VisitPrePostIncDec(E, false, false); |
| 86 | } |
| 87 | ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) { |
| 88 | return VisitPrePostIncDec(E, true, false); |
| 89 | } |
| 90 | ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) { |
| 91 | return VisitPrePostIncDec(E, false, true); |
| 92 | } |
| 93 | ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) { |
| 94 | return VisitPrePostIncDec(E, true, true); |
| 95 | } |
| 96 | ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 97 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
| 98 | return Visit(E->getSubExpr()); |
| 99 | } |
| 100 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 101 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
| 102 | // LNot,SizeOf,AlignOf,Real,Imag never return complex. |
| 103 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 104 | return Visit(E->getSubExpr()); |
| 105 | } |
| 106 | |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 107 | ComplexPairTy VisitBinMul (const BinaryOperator *E); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 108 | ComplexPairTy VisitBinAdd (const BinaryOperator *E); |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 109 | ComplexPairTy VisitBinSub (const BinaryOperator *E); |
| 110 | // FIXME: div/rem |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 111 | // GCC rejects and/or/xor for integer complex. |
| 112 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 113 | |
| 114 | // No comparisons produce a complex result. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 115 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 116 | // FIXME: Compound assignment operators. |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 117 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 118 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 119 | |
| 120 | ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame^] | 121 | ComplexPairTy VisitChooseExpr(ChooseExpr *CE); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 122 | // case Expr::ChooseExprClass: |
| 123 | }; |
| 124 | } // end anonymous namespace. |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Utilities |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 130 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 131 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 132 | ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr, |
| 133 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 134 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 135 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 136 | // FIXME: It would be nice to make this "Ptr->getName()+realp" |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 137 | llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Zero, Zero, "realp"); |
| 138 | llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Zero, One, "imagp"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 140 | // FIXME: It would be nice to make this "Ptr->getName()+real" |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 141 | llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, "real"); |
| 142 | llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, "imag"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 143 | return ComplexPairTy(Real, Imag); |
| 144 | } |
| 145 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 146 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 147 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 148 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 149 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 150 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 151 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 152 | llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Zero, Zero, "real"); |
| 153 | llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Zero, One, "imag"); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 155 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 156 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 161 | //===----------------------------------------------------------------------===// |
| 162 | // Visitor Methods |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 165 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
| 166 | fprintf(stderr, "Unimplemented complex expr!\n"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 167 | E->dump(); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 168 | const llvm::Type *EltTy = |
| 169 | CGF.ConvertType(E->getType()->getAsComplexType()->getElementType()); |
| 170 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 171 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 174 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
| 175 | llvm::Value *AggPtr = CGF.EmitCallExpr(E).getAggregateAddr(); |
| 176 | return EmitLoadOfComplex(AggPtr, false); |
| 177 | } |
| 178 | |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 179 | ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
| 180 | bool isInc, bool isPre) { |
| 181 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 182 | // FIXME: Handle volatile! |
| 183 | ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), false); |
| 184 | |
| 185 | int AmountVal = isInc ? 1 : -1; |
| 186 | |
| 187 | llvm::Value *NextVal; |
| 188 | if (isa<llvm::IntegerType>(InVal.first->getType())) |
| 189 | NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal); |
| 190 | else |
| 191 | NextVal = llvm::ConstantFP::get(InVal.first->getType(), AmountVal); |
| 192 | |
| 193 | // Add the inc/dec to the real part. |
| 194 | NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); |
| 195 | |
| 196 | ComplexPairTy IncVal(NextVal, InVal.second); |
| 197 | |
| 198 | // Store the updated result through the lvalue. |
| 199 | EmitStoreOfComplex(IncVal, LV.getAddress(), false); /* FIXME: Volatile */ |
| 200 | |
| 201 | // If this is a postinc, return the value read from memory, otherwise use the |
| 202 | // updated value. |
| 203 | return isPre ? IncVal : InVal; |
| 204 | } |
| 205 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 206 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 207 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 208 | llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 209 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 210 | return ComplexPairTy(ResR, ResI); |
| 211 | } |
| 212 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 213 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 214 | // ~(a+ib) = a + i*-b |
| 215 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 216 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i"); |
| 217 | return ComplexPairTy(Op.first, ResI); |
| 218 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 219 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 220 | ComplexPairTy ComplexExprEmitter::VisitBinAdd(const BinaryOperator *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 221 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 222 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 223 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 224 | llvm::Value *ResR = Builder.CreateAdd(LHS.first, RHS.first, "add.r"); |
| 225 | llvm::Value *ResI = Builder.CreateAdd(LHS.second, RHS.second, "add.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 226 | |
| 227 | return ComplexPairTy(ResR, ResI); |
| 228 | } |
| 229 | |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 230 | ComplexPairTy ComplexExprEmitter::VisitBinSub(const BinaryOperator *E) { |
| 231 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 232 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 233 | |
| 234 | llvm::Value *ResR = Builder.CreateSub(LHS.first, RHS.first, "sub.r"); |
| 235 | llvm::Value *ResI = Builder.CreateSub(LHS.second, RHS.second, "sub.i"); |
| 236 | |
| 237 | return ComplexPairTy(ResR, ResI); |
| 238 | } |
| 239 | |
| 240 | |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 241 | ComplexPairTy ComplexExprEmitter::VisitBinMul(const BinaryOperator *E) { |
| 242 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 243 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 244 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 245 | llvm::Value *ResRl = Builder.CreateMul(LHS.first, RHS.first, "mul.rl"); |
| 246 | llvm::Value *ResRr = Builder.CreateMul(LHS.second, RHS.second, "mul.rr"); |
| 247 | llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 249 | llvm::Value *ResIl = Builder.CreateMul(LHS.second, RHS.first, "mul.il"); |
| 250 | llvm::Value *ResIr = Builder.CreateMul(LHS.first, RHS.second, "mul.ir"); |
| 251 | llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 252 | |
| 253 | return ComplexPairTy(ResR, ResI); |
| 254 | } |
| 255 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 256 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 257 | assert(E->getLHS()->getType().getCanonicalType() == |
| 258 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
| 259 | // Emit the RHS. |
| 260 | ComplexPairTy Val = Visit(E->getRHS()); |
| 261 | |
| 262 | // Compute the address to store into. |
| 263 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
| 264 | |
| 265 | // Store into it. |
| 266 | // FIXME: Volatility! |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 267 | EmitStoreOfComplex(Val, LHS.getAddress(), false); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 268 | return Val; |
| 269 | } |
| 270 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 271 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 272 | CGF.EmitStmt(E->getLHS()); |
| 273 | return Visit(E->getRHS()); |
| 274 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 275 | |
| 276 | ComplexPairTy ComplexExprEmitter:: |
| 277 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 278 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 279 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 280 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 281 | |
| 282 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 283 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 284 | |
| 285 | CGF.EmitBlock(LHSBlock); |
| 286 | |
| 287 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 288 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 289 | |
| 290 | ComplexPairTy LHS = Visit(E->getLHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 291 | Builder.CreateBr(ContBlock); |
| 292 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 293 | |
| 294 | CGF.EmitBlock(RHSBlock); |
| 295 | |
| 296 | ComplexPairTy RHS = Visit(E->getRHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 297 | Builder.CreateBr(ContBlock); |
| 298 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 299 | |
| 300 | CGF.EmitBlock(ContBlock); |
| 301 | |
| 302 | // Create a PHI node for the real part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 303 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 304 | RealPN->reserveOperandSpace(2); |
| 305 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 306 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 307 | |
| 308 | // Create a PHI node for the imaginary part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 309 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 310 | ImagPN->reserveOperandSpace(2); |
| 311 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 312 | ImagPN->addIncoming(RHS.second, RHSBlock); |
| 313 | |
| 314 | return ComplexPairTy(RealPN, ImagPN); |
| 315 | } |
| 316 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame^] | 317 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
| 318 | llvm::APSInt CondVal(32); |
| 319 | bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext()); |
| 320 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 321 | |
| 322 | // Emit the LHS or RHS as appropriate. |
| 323 | return Visit(CondVal != 0 ? E->getLHS() : E->getRHS()); |
| 324 | } |
| 325 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 326 | //===----------------------------------------------------------------------===// |
| 327 | // Entry Point into this File |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | |
| 330 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 331 | /// complex type, ignoring the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 332 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 333 | assert(E && E->getType()->isComplexType() && |
| 334 | "Invalid complex expression to emit"); |
| 335 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 336 | return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 339 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 340 | /// of complex type, storing into the specified Value*. |
| 341 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
| 342 | llvm::Value *DestAddr) { |
| 343 | assert(E && E->getType()->isComplexType() && |
| 344 | "Invalid complex expression to emit"); |
| 345 | ComplexExprEmitter Emitter(*this); |
| 346 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
| 347 | Emitter.EmitStoreOfComplex(Val, DestAddr, false); |
| 348 | } |