[cfe-commits] r41429 - /cfe/trunk/CodeGen/CGExprComplex.cpp

Chris Lattner sabre at nondot.org
Sat Aug 25 22:57:58 PDT 2007


Author: lattner
Date: Sun Aug 26 00:57:57 2007
New Revision: 41429

URL: http://llvm.org/viewvc/llvm-project?rev=41429&view=rev
Log:
Implement casts from scalar -> complex and complex->complex.


Modified:
    cfe/trunk/CodeGen/CGExprComplex.cpp

Modified: cfe/trunk/CodeGen/CGExprComplex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprComplex.cpp?rev=41429&r1=41428&r2=41429&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExprComplex.cpp (original)
+++ cfe/trunk/CodeGen/CGExprComplex.cpp Sun Aug 26 00:57:57 2007
@@ -68,15 +68,24 @@
   }
   ComplexPairTy VisitExpr(Expr *S);
   ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
-  ComplexPairTy VisitImaginaryLiteral(ImaginaryLiteral *IL);
+  ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
   
   // l-values.
-  ComplexPairTy VisitDeclRefExpr(Expr *E) { return EmitLoadOfLValue(E); }
+  ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); }
   ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
-  ComplexPairTy VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); }
+  ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
 
   // FIXME: CompoundLiteralExpr
-  // FIXME: ImplicitCastExpr
+  
+  ComplexPairTy EmitCast(Expr *Op, QualType DestTy);
+  ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
+    // Unlike for scalars, we don't have to worry about function->ptr demotion
+    // here.
+    return EmitCast(E->getSubExpr(), E->getType());
+  }
+  ComplexPairTy VisitCastExpr(CastExpr *E) {
+    return EmitCast(E->getSubExpr(), E->getType());
+  }
   ComplexPairTy VisitCallExpr(const CallExpr *E);
   
   // Operators.
@@ -171,7 +180,8 @@
   return ComplexPairTy(U, U);
 }
 
-ComplexPairTy ComplexExprEmitter::VisitImaginaryLiteral(ImaginaryLiteral *IL) {
+ComplexPairTy ComplexExprEmitter::
+VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
   llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
   return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
 }
@@ -182,6 +192,36 @@
   return EmitLoadOfComplex(AggPtr, false);
 }
 
+ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) {
+  // Get the destination element type.
+  DestTy = cast<ComplexType>(DestTy.getCanonicalType())->getElementType();
+
+  // Two cases here: cast from (complex to complex) and (scalar to complex).
+  if (const ComplexType *CT = Op->getType()->getAsComplexType()) {
+    // C99 6.3.1.6: When a value of complextype is converted to another
+    // complex type, both the real and imaginary parts followthe conversion
+    // rules for the corresponding real types.
+    ComplexPairTy Res = Visit(Op);
+    QualType SrcEltTy = CT->getElementType();
+    Res.first = CGF.EmitConversion(RValue::get(Res.first), SrcEltTy,
+                                   DestTy).getVal();
+    Res.second = CGF.EmitConversion(RValue::get(Res.second), SrcEltTy,
+                                    DestTy).getVal();
+    return Res;
+  }
+  // C99 6.3.1.7: When a value of real type is converted to a complex type, the
+  // real part of the complex  result value is determined by the rules of
+  // conversion to the corresponding real type and the imaginary part of the
+  // complex result value is a positive zero or an unsigned zero.
+  llvm::Value *Elt = CGF.EmitScalarExpr(Op);
+
+  // Convert the input element to the element type of the complex.
+  Elt = CGF.EmitConversion(RValue::get(Elt), Op->getType(), DestTy).getVal();
+  
+  // Return (realval, 0).
+  return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType()));
+}
+
 ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
                                                      bool isInc, bool isPre) {
   LValue LV = CGF.EmitLValue(E->getSubExpr());





More information about the cfe-commits mailing list