[cfe-commits] r49201 - in /cfe/trunk/lib/CodeGen: CGDecl.cpp CGExpr.cpp CGExprScalar.cpp CodeGenFunction.cpp CodeGenFunction.h

Chris Lattner sabre at nondot.org
Thu Apr 3 21:07:36 PDT 2008


Author: lattner
Date: Thu Apr  3 23:07:35 2008
New Revision: 49201

URL: http://llvm.org/viewvc/llvm-project?rev=49201&view=rev
Log:
Codegen assignment to self correctly, patch by David Chisnall!

Modified:
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=49201&r1=49200&r2=49201&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Thu Apr  3 23:07:35 2008
@@ -83,8 +83,8 @@
   assert(Init && "Unable to create initialiser for static decl");
   
   std::string ContextName;
-  if (CurFuncDecl)
-    ContextName = CurFuncDecl->getName();
+  if (const FunctionDecl * FD = dyn_cast<FunctionDecl>(CurFuncDecl))
+    ContextName = FD->getName();
   else
     assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
   

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Thu Apr  3 23:07:35 2008
@@ -386,7 +386,13 @@
 }
 
 LValue CodeGenFunction::EmitPreDefinedLValue(const PreDefinedExpr *E) {
-  std::string FunctionName(CurFuncDecl->getName());
+  std::string FunctionName;
+  if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
+    FunctionName = FD->getName();
+  }
+  else {
+    assert(0 && "Attempting to load predefined constant for invalid decl type");
+  }
   std::string GlobalVarName;
   
   switch (E->getIdentType()) {
@@ -404,7 +410,7 @@
       break;
   }
   
-  GlobalVarName += CurFuncDecl->getName();
+  GlobalVarName += FunctionName;
   
   // FIXME: Can cache/reuse these within the module.
   llvm::Constant *C=llvm::ConstantArray::get(FunctionName);
@@ -581,8 +587,10 @@
     
   // Get object pointer and coerce object pointer to correct type.
   llvm::Value *Object = EmitLValue(E->getBase()).getAddress();
+  Object = Builder.CreateLoad(Object, E->getDecl()->getName());
   if (Object->getType() != ObjectType)
     Object = Builder.CreateBitCast(Object, ObjectType);
+
   
   // Return a pointer to the right element.
   return LValue::MakeAddr(Builder.CreateStructGEP(Object, Index,

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=49201&r1=49200&r2=49201&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Thu Apr  3 23:07:35 2008
@@ -484,8 +484,7 @@
 
   llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0);
   return Runtime->generateMessageSend(Builder, ConvertType(E->getType()),
-                                      // FIXME: Self can be assigned to!
-                                      CGF.CurFn->arg_begin(),
+                                      CGF.LoadObjCSelf(),
                                       Receiver, SelPtr,
                                       &Args[0], Args.size());
 }

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=49201&r1=49200&r2=49201&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Thu Apr  3 23:07:35 2008
@@ -97,7 +97,13 @@
   // TODO: Add something to AST to let the runtime specify the names and types
   // of these.
   llvm::Value *&DMEntry = LocalDeclMap[&(*OMD->getSelfDecl())];
-  DMEntry = AI;
+  const llvm::Type *SelfTy = AI->getType();
+  llvm::Value *DeclPtr = new llvm::AllocaInst(SelfTy, 0, "self.addr",
+                                   AllocaInsertPt);
+
+  // Store the initial value into the alloca.
+  Builder.CreateStore(AI, DeclPtr);
+  DMEntry = DeclPtr;
   ++AI; ++AI;
 
 
@@ -130,13 +136,22 @@
   assert(!verifyFunction(*CurFn) && "Generated method is not well formed.");
 }
 
+llvm::Value *CodeGenFunction::LoadObjCSelf(void)
+{
+  if(const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) {
+    llvm::Value *SelfPtr = LocalDeclMap[&(*OMD->getSelfDecl())];
+    return Builder.CreateLoad(SelfPtr, "self");
+  }
+  return NULL;
+}
+
 void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
   LLVMIntTy = ConvertType(getContext().IntTy);
   LLVMPointerWidth = static_cast<unsigned>(
     getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy)));
   
   CurFuncDecl = FD;
-  FnRetTy = CurFuncDecl->getType()->getAsFunctionType()->getResultType();
+  FnRetTy = FD->getType()->getAsFunctionType()->getResultType();
 
 
   CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Thu Apr  3 23:07:35 2008
@@ -246,7 +246,8 @@
   typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
   llvm::LLVMFoldingBuilder Builder;
   
-  const FunctionDecl *CurFuncDecl;
+  // Holds the Decl for the current function or method
+  const Decl *CurFuncDecl;
   QualType FnRetTy;
   llvm::Function *CurFn;
 
@@ -293,6 +294,8 @@
   void GenerateCode(const FunctionDecl *FD);
   
   const llvm::Type *ConvertType(QualType T);
+
+  llvm::Value *LoadObjCSelf();
   
   /// hasAggregateLLVMType - Return true if the specified AST type will map into
   /// an aggregate LLVM type or is void.





More information about the cfe-commits mailing list