[cfe-commits] r39622 - in /cfe/cfe/trunk: CodeGen/CGExpr.cpp CodeGen/CodeGenFunction.cpp CodeGen/CodeGenFunction.h include/clang/AST/Expr.h include/clang/Basic/TargetInfo.h

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


Author: clattner
Date: Wed Jul 11 11:46:20 2007
New Revision: 39622

URL: http://llvm.org/viewvc/llvm-project?rev=39622&view=rev
Log:
Implement array subscripts for non-vla types.

Modified:
    cfe/cfe/trunk/CodeGen/CGExpr.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp
    cfe/cfe/trunk/CodeGen/CodeGenFunction.h
    cfe/cfe/trunk/include/clang/AST/Expr.h
    cfe/cfe/trunk/include/clang/Basic/TargetInfo.h

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:46:20 2007
@@ -216,6 +216,8 @@
     
   case Expr::UnaryOperatorClass: 
     return EmitUnaryOpLValue(cast<UnaryOperator>(E));
+  case Expr::ArraySubscriptExprClass:
+    return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
   }
 }
 
@@ -291,6 +293,36 @@
   return LValue::getAddr(C);
 }
 
+LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
+  // The base and index must be pointers or integers, neither of which are
+  // aggregates.  Emit them.
+  QualType BaseTy;
+  Value *Base =EmitExprWithUsualUnaryConversions(E->getBase(), BaseTy).getVal();
+  QualType IdxTy;
+  Value *Idx = EmitExprWithUsualUnaryConversions(E->getIdx(), IdxTy).getVal();
+  
+  // Usually the base is the pointer type, but sometimes it is the index.
+  // Canonicalize to have the pointer as the base.
+  if (isa<llvm::PointerType>(Idx->getType())) {
+    std::swap(Base, Idx);
+    std::swap(BaseTy, IdxTy);
+  }
+  
+  // The pointer is now the base.  Extend or truncate the index type to 32 or
+  // 64-bits.
+  bool IdxSigned = IdxTy->isSignedIntegerType();
+  unsigned IdxBitwidth = cast<IntegerType>(Idx->getType())->getBitWidth();
+  if (IdxBitwidth != LLVMPointerWidth)
+    Idx = Builder.CreateIntCast(Idx, IntegerType::get(LLVMPointerWidth),
+                                IdxSigned, "idxprom");
+
+  // We know that the pointer points to a type of the correct size, unless the
+  // size is a VLA.
+  if (!E->getType()->isConstantSizeType())
+    assert(0 && "VLA idx not implemented");
+  return LValue::getAddr(Builder.CreateGEP(Base, Idx, "arrayidx"));
+}
+
 //===--------------------------------------------------------------------===//
 //                             Expression Emission
 //===--------------------------------------------------------------------===//
@@ -307,6 +339,7 @@
   // l-values.
   case Expr::DeclRefExprClass:
     // FIXME: EnumConstantDecl's are not lvalues.  This is wrong for them.
+  case Expr::ArraySubscriptExprClass:
     return EmitLoadOfLValue(E);
   case Expr::StringLiteralClass:
     return RValue::get(EmitLValue(E).getAddress());

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.cpp Wed Jul 11 11:46:20 2007
@@ -139,6 +139,7 @@
 
 void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
   LLVMIntTy = ConvertType(getContext().IntTy, FD->getLocation());
+  LLVMPointerWidth = Target.getPointerWidth(FD->getLocation());
   
   const llvm::FunctionType *Ty = 
     cast<llvm::FunctionType>(ConvertType(FD->getType(), FD->getLocation()));

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

==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:46:20 2007
@@ -45,6 +45,7 @@
   class CastExpr;
   class UnaryOperator;
   class BinaryOperator;
+  class ArraySubscriptExpr;
   
   class BlockVarDecl;
   class EnumConstantDecl;
@@ -127,6 +128,7 @@
   llvm::Instruction *AllocaInsertPt;
   
   const llvm::Type *LLVMIntTy;
+  unsigned LLVMPointerWidth;
   
   /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
   /// decls.
@@ -227,6 +229,7 @@
   LValue EmitDeclRefLValue(const DeclRefExpr *E);
   LValue EmitStringLiteralLValue(const StringLiteral *E);
   LValue EmitUnaryOpLValue(const UnaryOperator *E);
+  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
     
   //===--------------------------------------------------------------------===//
   //                             Expression Emission

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=39622&r1=39621&r2=39622&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Expr.h Wed Jul 11 11:46:20 2007
@@ -324,8 +324,10 @@
     Expr(ArraySubscriptExprClass, t),
     Base(base), Idx(idx), Loc(l) {}
   
-  Expr *getBase() const { return Base; }
+  Expr *getBase() { return Base; }
+  const Expr *getBase() const { return Base; }
   Expr *getIdx() { return Idx; }
+  const Expr *getIdx() const { return Idx; }
   SourceRange getSourceRange() const { 
     return SourceRange(Base->getLocStart(), Loc);
   }

Modified: cfe/cfe/trunk/include/clang/Basic/TargetInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/TargetInfo.h?rev=39622&r1=39621&r2=39622&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/TargetInfo.h (original)
+++ cfe/cfe/trunk/include/clang/Basic/TargetInfo.h Wed Jul 11 11:46:20 2007
@@ -104,6 +104,12 @@
     return true;
   }
   
+  /// getPointerWidth - Return the width of pointers on this target, we
+  /// currently assume one pointer type.
+  unsigned getPointerWidth(SourceLocation Loc) {
+    return 32;   // FIXME: implement correctly.
+  }
+  
   /// getBoolWidth - Return the size of '_Bool' and C++ 'bool' for this target,
   /// in bits.  
   unsigned getBoolWidth(SourceLocation Loc) {





More information about the cfe-commits mailing list