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