[cfe-commits] r39597 - in /cfe/cfe/trunk: CodeGen/CGExpr.cpp CodeGen/CodeGenFunction.h Sema/SemaExpr.cpp

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:46:05 PDT 2007


Author: clattner
Date: Wed Jul 11 11:46:04 2007
New Revision: 39597

URL: http://llvm.org/viewvc/llvm-project?rev=39597&view=rev
Log:
implement codegen of string literals.

Modified:
    cfe/cfe/trunk/CodeGen/CGExpr.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.h
    cfe/cfe/trunk/Sema/SemaExpr.cpp

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:46:04 2007
@@ -15,6 +15,8 @@
 #include "clang/AST/AST.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
+#include "llvm/Function.h"
+#include "llvm/GlobalVariable.h"
 using namespace llvm;
 using namespace clang;
 using namespace CodeGen;
@@ -209,7 +211,8 @@
 
   case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
   case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
-    
+  case Expr::StringLiteralClass:
+    return EmitStringLiteralLValue(cast<StringLiteral>(E));
     
   case Expr::UnaryOperatorClass: 
     return EmitUnaryOpLValue(cast<UnaryOperator>(E));
@@ -271,6 +274,23 @@
   return LValue::getAddr(EmitExpr(E->getSubExpr()).getVal());
 }
 
+LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
+  assert(!E->isWide() && "FIXME: Wide strings not supported yet!");
+  const char *StrData = E->getStrData();
+  unsigned Len = E->getByteLength();
+  
+  // FIXME: Can cache/reuse these within the module.
+  Constant *C = llvm::ConstantArray::get(std::string(StrData, StrData+Len));
+  
+  // Create a global variable for this.
+  C = new llvm::GlobalVariable(C->getType(), true, GlobalValue::InternalLinkage,
+                               C, ".str", CurFn->getParent());
+  Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
+  Constant *Zeros[] = { Zero, Zero };
+  C = ConstantExpr::getGetElementPtr(C, Zeros, 2);
+  return LValue::getAddr(C);
+}
+
 //===--------------------------------------------------------------------===//
 //                             Expression Emission
 //===--------------------------------------------------------------------===//
@@ -288,6 +308,8 @@
   case Expr::DeclRefExprClass:
     // FIXME: EnumConstantDecl's are not lvalues.  This is wrong for them.
     return EmitLoadOfLValue(E);
+  case Expr::StringLiteralClass:
+    return RValue::get(EmitLValue(E).getAddress());
     
   // Leaf expressions.
   case Expr::IntegerLiteralClass:

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:46:04 2007
@@ -40,6 +40,7 @@
   
   class Expr;
   class DeclRefExpr;
+  class StringLiteral;
   class IntegerLiteral;
   class CastExpr;
   class UnaryOperator;
@@ -224,6 +225,7 @@
   void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
   
   LValue EmitDeclRefLValue(const DeclRefExpr *E);
+  LValue EmitStringLiteralLValue(const StringLiteral *E);
   LValue EmitUnaryOpLValue(const UnaryOperator *E);
     
   //===--------------------------------------------------------------------===//

Modified: cfe/cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaExpr.cpp?rev=39597&r1=39596&r2=39597&view=diff

==============================================================================
--- cfe/cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaExpr.cpp Wed Jul 11 11:46:04 2007
@@ -46,7 +46,6 @@
   // FIXME: handle wchar_t
   QualType t = Context.getPointerType(Context.CharTy);
   
-  // FIXME: use factory.
   // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
   return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), 
                            Literal.AnyWide, t, StringToks[0].getLocation(),





More information about the cfe-commits mailing list