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 | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 75 | |
| 76 | // Operators. |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 77 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
| 78 | return Visit(E->getSubExpr()); |
| 79 | } |
| 80 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame^] | 81 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
| 82 | // LNot,SizeOf,AlignOf,Real,Imag never return complex. |
| 83 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 84 | return Visit(E->getSubExpr()); |
| 85 | } |
| 86 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 87 | // case Expr::ImplicitCastExprClass: |
| 88 | // case Expr::CastExprClass: |
| 89 | // case Expr::CallExprClass: |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 90 | ComplexPairTy VisitBinMul (const BinaryOperator *E); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 91 | ComplexPairTy VisitBinAdd (const BinaryOperator *E); |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 92 | // FIXME: div/rem |
| 93 | // GCC rejects and/or/xor for integer complex. |
| 94 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 95 | |
| 96 | // No comparisons produce a complex result. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 97 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
| 98 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 99 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 100 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 101 | |
| 102 | ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO); |
| 103 | // case Expr::ChooseExprClass: |
| 104 | }; |
| 105 | } // end anonymous namespace. |
| 106 | |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | // Utilities |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 111 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 112 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 113 | ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr, |
| 114 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 115 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 116 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
| 117 | // FIXME: It would be nice to make this "Ptr->getName()+realp" |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 118 | llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Zero, Zero, "realp"); |
| 119 | llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Zero, One, "imagp"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 121 | // FIXME: It would be nice to make this "Ptr->getName()+real" |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 122 | llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, "real"); |
| 123 | llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, "imag"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 124 | return ComplexPairTy(Real, Imag); |
| 125 | } |
| 126 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 127 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 128 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 129 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 130 | bool isVolatile) { |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 131 | llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 132 | llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 133 | llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Zero, Zero, "real"); |
| 134 | llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Zero, One, "imag"); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 136 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 137 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // Visitor Methods |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 146 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
| 147 | fprintf(stderr, "Unimplemented complex expr!\n"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 148 | E->dump(); |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 149 | const llvm::Type *EltTy = |
| 150 | CGF.ConvertType(E->getType()->getAsComplexType()->getElementType()); |
| 151 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 152 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 155 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 156 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 157 | llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 158 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 159 | return ComplexPairTy(ResR, ResI); |
| 160 | } |
| 161 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame^] | 162 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 163 | // ~(a+ib) = a + i*-b |
| 164 | ComplexPairTy Op = Visit(E->getSubExpr()); |
| 165 | llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i"); |
| 166 | return ComplexPairTy(Op.first, ResI); |
| 167 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 168 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 169 | ComplexPairTy ComplexExprEmitter::VisitBinAdd(const BinaryOperator *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 170 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 171 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 172 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 173 | llvm::Value *ResR = Builder.CreateAdd(LHS.first, RHS.first, "add.r"); |
| 174 | llvm::Value *ResI = Builder.CreateAdd(LHS.second, RHS.second, "add.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 175 | |
| 176 | return ComplexPairTy(ResR, ResI); |
| 177 | } |
| 178 | |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 179 | ComplexPairTy ComplexExprEmitter::VisitBinMul(const BinaryOperator *E) { |
| 180 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 181 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 182 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 183 | llvm::Value *ResRl = Builder.CreateMul(LHS.first, RHS.first, "mul.rl"); |
| 184 | llvm::Value *ResRr = Builder.CreateMul(LHS.second, RHS.second, "mul.rr"); |
| 185 | llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 187 | llvm::Value *ResIl = Builder.CreateMul(LHS.second, RHS.first, "mul.il"); |
| 188 | llvm::Value *ResIr = Builder.CreateMul(LHS.first, RHS.second, "mul.ir"); |
| 189 | llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 190 | |
| 191 | return ComplexPairTy(ResR, ResI); |
| 192 | } |
| 193 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 194 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 195 | assert(E->getLHS()->getType().getCanonicalType() == |
| 196 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
| 197 | // Emit the RHS. |
| 198 | ComplexPairTy Val = Visit(E->getRHS()); |
| 199 | |
| 200 | // Compute the address to store into. |
| 201 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
| 202 | |
| 203 | // Store into it. |
| 204 | // FIXME: Volatility! |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 205 | EmitStoreOfComplex(Val, LHS.getAddress(), false); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 206 | return Val; |
| 207 | } |
| 208 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 209 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 210 | CGF.EmitStmt(E->getLHS()); |
| 211 | return Visit(E->getRHS()); |
| 212 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 213 | |
| 214 | ComplexPairTy ComplexExprEmitter:: |
| 215 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 216 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 217 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 218 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 219 | |
| 220 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 221 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 222 | |
| 223 | CGF.EmitBlock(LHSBlock); |
| 224 | |
| 225 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 226 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 227 | |
| 228 | ComplexPairTy LHS = Visit(E->getLHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 229 | Builder.CreateBr(ContBlock); |
| 230 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 231 | |
| 232 | CGF.EmitBlock(RHSBlock); |
| 233 | |
| 234 | ComplexPairTy RHS = Visit(E->getRHS()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 235 | Builder.CreateBr(ContBlock); |
| 236 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 237 | |
| 238 | CGF.EmitBlock(ContBlock); |
| 239 | |
| 240 | // Create a PHI node for the real part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 241 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 242 | RealPN->reserveOperandSpace(2); |
| 243 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 244 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 245 | |
| 246 | // Create a PHI node for the imaginary part. |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 247 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 248 | ImagPN->reserveOperandSpace(2); |
| 249 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 250 | ImagPN->addIncoming(RHS.second, RHSBlock); |
| 251 | |
| 252 | return ComplexPairTy(RealPN, ImagPN); |
| 253 | } |
| 254 | |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | // Entry Point into this File |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | |
| 259 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 260 | /// complex type, ignoring the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 261 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 262 | assert(E && E->getType()->isComplexType() && |
| 263 | "Invalid complex expression to emit"); |
| 264 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 265 | return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 266 | } |
| 267 | |