[cfe-commits] r39555 - in /cfe/cfe/trunk: CodeGen/CGExpr.cpp CodeGen/CodeGenFunction.h clang.xcodeproj/project.pbxproj include/clang/AST/Expr.h

clattner at cs.uiuc.edu clattner at cs.uiuc.edu
Wed Jul 11 09:45:33 PDT 2007


Author: clattner
Date: Wed Jul 11 11:45:33 2007
New Revision: 39555

URL: http://llvm.org/viewvc/llvm-project?rev=39555&view=rev
Log:
Implement scaffolding for lvalues.  Implement block vardecl lvalues.

This allows us to translate:

int func() {
  register int X;
  {
    int Y;
    return 1+X+Y;
  }
}

into:

define i32 @func() {
entry:
        %X = alloca i32         ; <i32*> [#uses=1]
        %Y = alloca i32         ; <i32*> [#uses=1]
        %allocapt = bitcast i32 undef to i32            ; <i32> [#uses=0]
        %tmp = load i32* %X             ; <i32> [#uses=1]
        %tmp1 = add i32 1, %tmp         ; <i32> [#uses=1]
        %tmp2 = load i32* %Y            ; <i32> [#uses=1]
        %tmp3 = add i32 %tmp1, %tmp2            ; <i32> [#uses=1]
        ret i32 %tmp3
                ; No predecessors!
        ret i32 undef
}

Modified:
    cfe/cfe/trunk/CodeGen/CGExpr.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.h
    cfe/cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/cfe/trunk/include/clang/AST/Expr.h

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:45:33 2007
@@ -19,6 +19,32 @@
 using namespace clang;
 using namespace CodeGen;
 
+//===--------------------------------------------------------------------===//
+//                         LValue Expression Emission
+//===--------------------------------------------------------------------===//
+
+LValue CodeGenFunction::EmitLValue(const Expr *E) {
+  switch (E->getStmtClass()) {
+  default:
+    printf("Unimplemented lvalue expr!\n");
+    E->dump();
+    return LValue::getAddr(UndefValue::get(
+                              llvm::PointerType::get(llvm::Type::Int32Ty)));
+
+  case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E));
+  }
+}
+
+
+LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
+  const Decl *D = E->getDecl();
+  if (isa<BlockVarDecl>(D)) {
+    Value *V = LocalDeclMap[D];
+    assert(V && "BlockVarDecl not entered in LocalDeclMap?");
+    return LValue::getAddr(V);
+  }
+  assert(0 && "Unimp declref");
+}
 
 //===--------------------------------------------------------------------===//
 //                             Expression Emission
@@ -32,12 +58,24 @@
     printf("Unimplemented expr!\n");
     E->dump();
     return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty));
-  case Stmt::ParenExprClass:
-    return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
-  case Stmt::IntegerLiteralClass:
+    
+  // l-values.
+  case Expr::DeclRefExprClass: {
+    // FIXME: EnumConstantDecl's are not lvalues.
+    LValue LV = EmitLValue(E);
+    // FIXME: this is silly.
+    assert(!LV.isBitfield());
+    return ExprResult::get(Builder.CreateLoad(LV.getAddress(), "tmp"));
+  }
+    
+  // Leaf expressions.
+  case Expr::IntegerLiteralClass:
     return EmitIntegerLiteral(cast<IntegerLiteral>(E)); 
     
-  case Stmt::BinaryOperatorClass:
+  // Operators.  
+  case Expr::ParenExprClass:
+    return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
+  case Expr::BinaryOperatorClass:
     return EmitBinaryOperator(cast<BinaryOperator>(E));
   }
   

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:45:33 2007
@@ -36,6 +36,7 @@
   class DeclStmt;
   
   class Expr;
+  class DeclRefExpr;
   class IntegerLiteral;
   class BinaryOperator;
   
@@ -44,6 +45,11 @@
 namespace CodeGen {
   class CodeGenModule;
   
+
+/// ExprResult - This trivial value class is used to represent the result of an
+/// expression that is evaluated.  It can be one of two things: either a simple
+/// LLVM SSA value, or the address of an aggregate value in memory.  These two
+/// possibilities are discriminated by isAggregate/isScalar.
 class ExprResult {
   Value *V;
   // TODO: Encode this into the low bit of pointer for more efficient
@@ -79,7 +85,26 @@
     return ER;
   }
 };
+
+
+/// LValue - This represents an lvalue references.  Because C/C++ allow
+/// bitfields, this is not a simple LLVM pointer, it may be a pointer plus a
+/// bitrange.
+class LValue {
+  // FIXME: Volatility.  Restrict?
+  llvm::Value *V;
+public:
+  bool isBitfield() const { return false; }
+  
+  llvm::Value *getAddress() const { assert(!isBitfield()); return V; }
   
+  static LValue getAddr(Value *V) {
+    LValue R;
+    R.V = V;
+    return R;
+  }
+};
+
 /// CodeGenFunction - This class organizes the per-function state that is used
 /// while generating LLVM code.
 class CodeGenFunction {
@@ -137,6 +162,13 @@
   void EmitReturnStmt(const ReturnStmt &S);
   
   //===--------------------------------------------------------------------===//
+  //                         LValue Expression Emission
+  //===--------------------------------------------------------------------===//
+  
+  LValue EmitLValue(const Expr *E);
+  LValue EmitDeclRefLValue(const DeclRefExpr *E);
+    
+  //===--------------------------------------------------------------------===//
   //                             Expression Emission
   //===--------------------------------------------------------------------===//
 

Modified: cfe/cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=39555&r1=39554&r2=39555&view=diff

==============================================================================
--- cfe/cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/cfe/trunk/clang.xcodeproj/project.pbxproj Wed Jul 11 11:45:33 2007
@@ -39,6 +39,7 @@
 		DE3461270AFE68BE00DBC861 /* MinimalAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE3461260AFE68BE00DBC861 /* MinimalAction.cpp */; };
 		DE34621D0AFEB19B00DBC861 /* StmtPrinter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE34621C0AFEB19B00DBC861 /* StmtPrinter.cpp */; };
 		DE3464220B03040900DBC861 /* Type.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE3464210B03040900DBC861 /* Type.h */; };
+		DE4264FC0C113592005A861D /* CGDecl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE4264FB0C113592005A861D /* CGDecl.cpp */; };
 		DE46BF280AE0A82D00CC047C /* TargetInfo.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DE46BF270AE0A82D00CC047C /* TargetInfo.h */; };
 		DE4772FA0C10EAE5002239E8 /* CGStmt.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE4772F90C10EAE5002239E8 /* CGStmt.cpp */; };
 		DE4772FC0C10EAEC002239E8 /* CGExpr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */; };
@@ -198,6 +199,7 @@
 		DE3461260AFE68BE00DBC861 /* MinimalAction.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = MinimalAction.cpp; path = Parse/MinimalAction.cpp; sourceTree = "<group>"; };
 		DE34621C0AFEB19B00DBC861 /* StmtPrinter.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = StmtPrinter.cpp; path = AST/StmtPrinter.cpp; sourceTree = "<group>"; };
 		DE3464210B03040900DBC861 /* Type.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Type.h; path = clang/AST/Type.h; sourceTree = "<group>"; };
+		DE4264FB0C113592005A861D /* CGDecl.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = CGDecl.cpp; path = CodeGen/CGDecl.cpp; sourceTree = "<group>"; };
 		DE46BF270AE0A82D00CC047C /* TargetInfo.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = TargetInfo.h; sourceTree = "<group>"; };
 		DE4772F90C10EAE5002239E8 /* CGStmt.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = CGStmt.cpp; path = CodeGen/CGStmt.cpp; sourceTree = "<group>"; };
 		DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = CGExpr.cpp; path = CodeGen/CGExpr.cpp; sourceTree = "<group>"; };
@@ -374,6 +376,7 @@
 				DE928B820C0A616000231DA4 /* CodeGenFunction.cpp */,
 				DE928B7C0C0A615100231DA4 /* CodeGenModule.h */,
 				DE928B7E0C0A615600231DA4 /* CodeGenModule.cpp */,
+				DE4264FB0C113592005A861D /* CGDecl.cpp */,
 				DE4772FB0C10EAEC002239E8 /* CGExpr.cpp */,
 				DE4772F90C10EAE5002239E8 /* CGStmt.cpp */,
 				DE928B120C05659200231DA4 /* ModuleBuilder.cpp */,
@@ -607,6 +610,7 @@
 				DE928B830C0A616000231DA4 /* CodeGenFunction.cpp in Sources */,
 				DE4772FA0C10EAE5002239E8 /* CGStmt.cpp in Sources */,
 				DE4772FC0C10EAEC002239E8 /* CGExpr.cpp in Sources */,
+				DE4264FC0C113592005A861D /* CGDecl.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Modified: cfe/cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/Expr.h?rev=39555&r1=39554&r2=39555&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Expr.h Wed Jul 11 11:45:33 2007
@@ -109,7 +109,8 @@
   DeclRefExpr(Decl *d, QualType t, SourceLocation l) : 
     Expr(DeclRefExprClass, t), D(d), Loc(l) {}
   
-  Decl *getDecl() const { return D; }
+  Decl *getDecl() { return D; }
+  const Decl *getDecl() const { return D; }
   virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
   
   virtual void visit(StmtVisitor &Visitor);





More information about the cfe-commits mailing list