[cfe-commits] r39796 - in /cfe/trunk: AST/StmtPrinter.cpp CodeGen/CGExpr.cpp CodeGen/CodeGenFunction.h

Chris Lattner sabre at nondot.org
Thu Jul 12 22:18:12 PDT 2007


Author: lattner
Date: Fri Jul 13 00:18:11 2007
New Revision: 39796

URL: http://llvm.org/viewvc/llvm-project?rev=39796&view=rev
Log:
"Codegen for Character Literals and Conditional Operator

Both in one patch, and the test case that Chris didn't commit last
time is in there too...

I'll split the patch up if somebody wants it split."

Patch by Keith Bauer.


Modified:
    cfe/trunk/AST/StmtPrinter.cpp
    cfe/trunk/CodeGen/CGExpr.cpp
    cfe/trunk/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/StmtPrinter.cpp?rev=39796&r1=39795&r2=39796&view=diff

==============================================================================
--- cfe/trunk/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/AST/StmtPrinter.cpp Fri Jul 13 00:18:11 2007
@@ -17,6 +17,7 @@
 #include "clang/Lex/IdentifierTable.h"
 #include "llvm/Support/Compiler.h"
 #include <iostream>
+#include <iomanip>
 using namespace clang;
 
 //===----------------------------------------------------------------------===//
@@ -291,8 +292,14 @@
 }
 
 void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
-  // FIXME: print value.
-  OS << "x";
+  unsigned value = Node->getValue();
+  if (isprint(value)) {
+    OS << "'" << (char)value << "'";
+  } else {
+    // FIXME something to indicate this is a character literal?
+    OS << std::hex << std::setiosflags(std::ios_base::showbase) << value
+       << std::dec << std::resetiosflags(std::ios_base::showbase);
+  }
 }
 
 void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {

Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=39796&r1=39795&r2=39796&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Fri Jul 13 00:18:11 2007
@@ -464,6 +464,8 @@
     return EmitIntegerLiteral(cast<IntegerLiteral>(E)); 
   case Expr::FloatingLiteralClass:
     return EmitFloatingLiteral(cast<FloatingLiteral>(E));
+  case Expr::CharacterLiteralClass:
+    return EmitCharacterLiteral(cast<CharacterLiteral>(E));
     
   // Operators.  
   case Expr::ParenExprClass:
@@ -476,6 +478,9 @@
     return EmitCallExpr(cast<CallExpr>(E));
   case Expr::BinaryOperatorClass:
     return EmitBinaryOperator(cast<BinaryOperator>(E));
+  
+  case Expr::ConditionalOperatorClass:
+    return EmitConditionalOperator(cast<ConditionalOperator>(E));
   }
   
 }
@@ -487,7 +492,10 @@
   return RValue::get(llvm::ConstantFP::get(ConvertType(E->getType()),
                                            E->getValue()));
 }
-
+RValue CodeGenFunction::EmitCharacterLiteral(const CharacterLiteral *E) {
+  return RValue::get(llvm::ConstantInt::get(ConvertType(E->getType()),
+                                            E->getValue()));
+}
 
 RValue CodeGenFunction::EmitArraySubscriptExprRV(const ArraySubscriptExpr *E) {
   // Emit subscript expressions in rvalue context's.  For most cases, this just
@@ -1322,3 +1330,41 @@
   EmitExpr(E->getLHS());
   return EmitExpr(E->getRHS());
 }
+
+RValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator *E) {
+  llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
+  llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
+  llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
+  
+  llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
+  Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
+  
+  // FIXME: LHS & RHS need the "usual arithmetic conversions" but
+  // that's not possible with the current design.
+  
+  EmitBlock(LHSBlock);
+  QualType LHSTy;
+  llvm::Value *LHSValue = E->getLHS() ? // GNU extension
+      EmitExprWithUsualUnaryConversions(E->getLHS(), LHSTy).getVal() :
+      Cond;
+  Builder.CreateBr(ContBlock);
+  LHSBlock = Builder.GetInsertBlock();
+  
+  EmitBlock(RHSBlock);
+  QualType RHSTy;
+  llvm::Value *RHSValue =
+    EmitExprWithUsualUnaryConversions(E->getRHS(), RHSTy).getVal();
+  Builder.CreateBr(ContBlock);
+  RHSBlock = Builder.GetInsertBlock();
+  
+  const llvm::Type *LHSType = LHSValue->getType();
+  assert(LHSType == RHSValue->getType() && "?: LHS & RHS must have same type");
+  
+  EmitBlock(ContBlock);
+  llvm::PHINode *PN = Builder.CreatePHI(LHSType, "cond");
+  PN->reserveOperandSpace(2);
+  PN->addIncoming(LHSValue, LHSBlock);
+  PN->addIncoming(RHSValue, RHSBlock);
+  
+  return RValue::get(PN);
+}

Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=39796&r1=39795&r2=39796&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Fri Jul 13 00:18:11 2007
@@ -46,12 +46,14 @@
   class StringLiteral;
   class IntegerLiteral;
   class FloatingLiteral;
+  class CharacterLiteral;
   class CastExpr;
   class CallExpr;
   class UnaryOperator;
   class BinaryOperator;
   class CompoundAssignOperator;
   class ArraySubscriptExpr;
+  class ConditionalOperator;
   
   class BlockVarDecl;
   class EnumConstantDecl;
@@ -309,7 +311,8 @@
   RValue EmitExpr(const Expr *E);
   RValue EmitIntegerLiteral(const IntegerLiteral *E);
   RValue EmitFloatingLiteral(const FloatingLiteral *E);
-  
+  RValue EmitCharacterLiteral(const CharacterLiteral *E);
+   
   RValue EmitCastExpr(const CastExpr *E);
   RValue EmitCallExpr(const CallExpr *E);
   RValue EmitArraySubscriptExprRV(const ArraySubscriptExpr *E);
@@ -351,6 +354,9 @@
   
   RValue EmitBinaryAssign(const BinaryOperator *E);
   RValue EmitBinaryComma(const BinaryOperator *E);
+  
+  // Conditional Operator.
+  RValue EmitConditionalOperator(const ConditionalOperator *E);
 };
 }  // end namespace CodeGen
 }  // end namespace clang





More information about the cfe-commits mailing list