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; |
| 33 | public: |
| 34 | ComplexExprEmitter(CodeGenFunction &cgf) : CGF(cgf) { |
| 35 | } |
| 36 | |
| 37 | |
| 38 | //===--------------------------------------------------------------------===// |
| 39 | // Utilities |
| 40 | //===--------------------------------------------------------------------===// |
| 41 | |
| 42 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 43 | /// value l-value, this method emits the address of the l-value, then loads |
| 44 | /// and returns the result. |
| 45 | ComplexPairTy EmitLoadOfLValue(const Expr *E); |
| 46 | |
| 47 | |
| 48 | //===--------------------------------------------------------------------===// |
| 49 | // Visitor Methods |
| 50 | //===--------------------------------------------------------------------===// |
| 51 | |
| 52 | ComplexPairTy VisitStmt(Stmt *S) { |
| 53 | fprintf(stderr, "Unimplemented agg expr!\n"); |
| 54 | S->dump(); |
| 55 | return ComplexPairTy(); |
| 56 | } |
| 57 | ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());} |
| 58 | |
| 59 | // l-values. |
| 60 | ComplexPairTy VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 61 | return EmitLoadOfLValue(DRE); |
| 62 | } |
| 63 | // case Expr::ArraySubscriptExprClass: |
| 64 | |
| 65 | // Operators. |
| 66 | // case Expr::UnaryOperatorClass: |
| 67 | // case Expr::ImplicitCastExprClass: |
| 68 | // case Expr::CastExprClass: |
| 69 | // case Expr::CallExprClass: |
| 70 | ComplexPairTy VisitBinaryOperator(const BinaryOperator *BO); |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 71 | ComplexPairTy VisitBinMul (const BinaryOperator *E); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 72 | ComplexPairTy VisitBinAdd (const BinaryOperator *E); |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 73 | |
| 74 | // No comparisons produce a complex result. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 75 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
| 76 | |
| 77 | |
| 78 | ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO); |
| 79 | // case Expr::ChooseExprClass: |
| 80 | }; |
| 81 | } // end anonymous namespace. |
| 82 | |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | // Utilities |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | |
| 87 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 88 | /// value l-value, this method emits the address of the l-value, then loads |
| 89 | /// and returns the result. |
| 90 | ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(const Expr *E) { |
| 91 | LValue LV = CGF.EmitLValue(E); |
| 92 | assert(LV.isSimple() && "Can't have complex bitfield, vector, etc"); |
| 93 | |
| 94 | // Load the real/imag values. |
| 95 | llvm::Value *Real, *Imag; |
| 96 | CGF.EmitLoadOfComplex(LV.getAddress(), Real, Imag); |
| 97 | return ComplexPairTy(Real, Imag); |
| 98 | } |
| 99 | |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | // Visitor Methods |
| 102 | //===----------------------------------------------------------------------===// |
| 103 | |
| 104 | ComplexPairTy ComplexExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 105 | fprintf(stderr, "Unimplemented complex binary expr!\n"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 106 | E->dump(); |
| 107 | return ComplexPairTy(); |
| 108 | #if 0 |
| 109 | switch (E->getOpcode()) { |
| 110 | default: |
| 111 | return; |
| 112 | case BinaryOperator::Mul: |
| 113 | LHS = EmitExpr(E->getLHS()); |
| 114 | RHS = EmitExpr(E->getRHS()); |
| 115 | return EmitMul(LHS, RHS, E->getType()); |
| 116 | case BinaryOperator::Div: |
| 117 | LHS = EmitExpr(E->getLHS()); |
| 118 | RHS = EmitExpr(E->getRHS()); |
| 119 | return EmitDiv(LHS, RHS, E->getType()); |
| 120 | case BinaryOperator::Rem: |
| 121 | LHS = EmitExpr(E->getLHS()); |
| 122 | RHS = EmitExpr(E->getRHS()); |
| 123 | return EmitRem(LHS, RHS, E->getType()); |
| 124 | case BinaryOperator::Add: |
| 125 | LHS = EmitExpr(E->getLHS()); |
| 126 | RHS = EmitExpr(E->getRHS()); |
| 127 | if (!E->getType()->isPointerType()) |
| 128 | return EmitAdd(LHS, RHS, E->getType()); |
| 129 | |
| 130 | return EmitPointerAdd(LHS, E->getLHS()->getType(), |
| 131 | RHS, E->getRHS()->getType(), E->getType()); |
| 132 | case BinaryOperator::Sub: |
| 133 | LHS = EmitExpr(E->getLHS()); |
| 134 | RHS = EmitExpr(E->getRHS()); |
| 135 | |
| 136 | if (!E->getLHS()->getType()->isPointerType()) |
| 137 | return EmitSub(LHS, RHS, E->getType()); |
| 138 | |
| 139 | return EmitPointerSub(LHS, E->getLHS()->getType(), |
| 140 | RHS, E->getRHS()->getType(), E->getType()); |
| 141 | case BinaryOperator::Shl: |
| 142 | LHS = EmitExpr(E->getLHS()); |
| 143 | RHS = EmitExpr(E->getRHS()); |
| 144 | return EmitShl(LHS, RHS, E->getType()); |
| 145 | case BinaryOperator::Shr: |
| 146 | LHS = EmitExpr(E->getLHS()); |
| 147 | RHS = EmitExpr(E->getRHS()); |
| 148 | return EmitShr(LHS, RHS, E->getType()); |
| 149 | case BinaryOperator::And: |
| 150 | LHS = EmitExpr(E->getLHS()); |
| 151 | RHS = EmitExpr(E->getRHS()); |
| 152 | return EmitAnd(LHS, RHS, E->getType()); |
| 153 | case BinaryOperator::Xor: |
| 154 | LHS = EmitExpr(E->getLHS()); |
| 155 | RHS = EmitExpr(E->getRHS()); |
| 156 | return EmitXor(LHS, RHS, E->getType()); |
| 157 | case BinaryOperator::Or : |
| 158 | LHS = EmitExpr(E->getLHS()); |
| 159 | RHS = EmitExpr(E->getRHS()); |
| 160 | return EmitOr(LHS, RHS, E->getType()); |
| 161 | case BinaryOperator::MulAssign: { |
| 162 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 163 | LValue LHSLV; |
| 164 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 165 | LHS = EmitMul(LHS, RHS, CAO->getComputationType()); |
| 166 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 167 | } |
| 168 | case BinaryOperator::DivAssign: { |
| 169 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 170 | LValue LHSLV; |
| 171 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 172 | LHS = EmitDiv(LHS, RHS, CAO->getComputationType()); |
| 173 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 174 | } |
| 175 | case BinaryOperator::RemAssign: { |
| 176 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 177 | LValue LHSLV; |
| 178 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 179 | LHS = EmitRem(LHS, RHS, CAO->getComputationType()); |
| 180 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 181 | } |
| 182 | case BinaryOperator::AddAssign: { |
| 183 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 184 | LValue LHSLV; |
| 185 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 186 | LHS = EmitAdd(LHS, RHS, CAO->getComputationType()); |
| 187 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 188 | } |
| 189 | case BinaryOperator::SubAssign: { |
| 190 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 191 | LValue LHSLV; |
| 192 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 193 | LHS = EmitSub(LHS, RHS, CAO->getComputationType()); |
| 194 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 195 | } |
| 196 | case BinaryOperator::ShlAssign: { |
| 197 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 198 | LValue LHSLV; |
| 199 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 200 | LHS = EmitShl(LHS, RHS, CAO->getComputationType()); |
| 201 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 202 | } |
| 203 | case BinaryOperator::ShrAssign: { |
| 204 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 205 | LValue LHSLV; |
| 206 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 207 | LHS = EmitShr(LHS, RHS, CAO->getComputationType()); |
| 208 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 209 | } |
| 210 | case BinaryOperator::AndAssign: { |
| 211 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 212 | LValue LHSLV; |
| 213 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 214 | LHS = EmitAnd(LHS, RHS, CAO->getComputationType()); |
| 215 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 216 | } |
| 217 | case BinaryOperator::OrAssign: { |
| 218 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 219 | LValue LHSLV; |
| 220 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 221 | LHS = EmitOr(LHS, RHS, CAO->getComputationType()); |
| 222 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 223 | } |
| 224 | case BinaryOperator::XorAssign: { |
| 225 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(E); |
| 226 | LValue LHSLV; |
| 227 | EmitCompoundAssignmentOperands(CAO, LHSLV, LHS, RHS); |
| 228 | LHS = EmitXor(LHS, RHS, CAO->getComputationType()); |
| 229 | return EmitCompoundAssignmentResult(CAO, LHSLV, LHS); |
| 230 | } |
| 231 | case BinaryOperator::Comma: return EmitBinaryComma(E); |
| 232 | } |
| 233 | #endif |
| 234 | } |
| 235 | |
| 236 | ComplexPairTy ComplexExprEmitter::VisitBinAdd(const BinaryOperator *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 237 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 238 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 239 | |
| 240 | llvm::Value *ResR = CGF.Builder.CreateAdd(LHS.first, RHS.first, "add.r"); |
| 241 | llvm::Value *ResI = CGF.Builder.CreateAdd(LHS.second, RHS.second, "add.i"); |
| 242 | |
| 243 | return ComplexPairTy(ResR, ResI); |
| 244 | } |
| 245 | |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 246 | ComplexPairTy ComplexExprEmitter::VisitBinMul(const BinaryOperator *E) { |
| 247 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 248 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 249 | |
| 250 | llvm::Value *ResRl = CGF.Builder.CreateMul(LHS.first, RHS.first, "mul.rl"); |
| 251 | llvm::Value *ResRr = CGF.Builder.CreateMul(LHS.second, RHS.second, "mul.rr"); |
| 252 | llvm::Value *ResR = CGF.Builder.CreateSub(ResRl, ResRr, "mul.r"); |
| 253 | |
| 254 | llvm::Value *ResIl = CGF.Builder.CreateMul(LHS.second, RHS.first, "mul.il"); |
| 255 | llvm::Value *ResIr = CGF.Builder.CreateMul(LHS.first, RHS.second, "mul.ir"); |
| 256 | llvm::Value *ResI = CGF.Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
| 257 | |
| 258 | return ComplexPairTy(ResR, ResI); |
| 259 | } |
| 260 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 261 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 262 | assert(E->getLHS()->getType().getCanonicalType() == |
| 263 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
| 264 | // Emit the RHS. |
| 265 | ComplexPairTy Val = Visit(E->getRHS()); |
| 266 | |
| 267 | // Compute the address to store into. |
| 268 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
| 269 | |
| 270 | // Store into it. |
| 271 | // FIXME: Volatility! |
| 272 | CGF.EmitStoreOfComplex(Val.first, Val.second, LHS.getAddress()); |
| 273 | return Val; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | ComplexPairTy ComplexExprEmitter:: |
| 278 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 279 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 280 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 281 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 282 | |
| 283 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
| 284 | CGF.Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
| 285 | |
| 286 | CGF.EmitBlock(LHSBlock); |
| 287 | |
| 288 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 289 | assert(E->getLHS() && "Must have LHS for complex value"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 290 | |
| 291 | ComplexPairTy LHS = Visit(E->getLHS()); |
| 292 | CGF.Builder.CreateBr(ContBlock); |
| 293 | LHSBlock = CGF.Builder.GetInsertBlock(); |
| 294 | |
| 295 | CGF.EmitBlock(RHSBlock); |
| 296 | |
| 297 | ComplexPairTy RHS = Visit(E->getRHS()); |
| 298 | CGF.Builder.CreateBr(ContBlock); |
| 299 | RHSBlock = CGF.Builder.GetInsertBlock(); |
| 300 | |
| 301 | CGF.EmitBlock(ContBlock); |
| 302 | |
| 303 | // Create a PHI node for the real part. |
| 304 | llvm::PHINode *RealPN = CGF.Builder.CreatePHI(LHS.first->getType(), "cond.r"); |
| 305 | RealPN->reserveOperandSpace(2); |
| 306 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 307 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 308 | |
| 309 | // Create a PHI node for the imaginary part. |
| 310 | llvm::PHINode *ImagPN = CGF.Builder.CreatePHI(LHS.first->getType(), "cond.i"); |
| 311 | ImagPN->reserveOperandSpace(2); |
| 312 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 313 | ImagPN->addIncoming(RHS.second, RHSBlock); |
| 314 | |
| 315 | return ComplexPairTy(RealPN, ImagPN); |
| 316 | } |
| 317 | |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // Entry Point into this File |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 323 | /// complex type, ignoring the result. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 324 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 325 | assert(E && E->getType()->isComplexType() && |
| 326 | "Invalid complex expression to emit"); |
| 327 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 328 | return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 329 | } |
| 330 | |