Sign-extend array index expressions to work correctly on non-32 bit machines.
authorVikram S. Adve <vadve@cs.uiuc.edu>
Sat, 24 Aug 2002 14:44:23 +0000 (14:44 +0000)
committerVikram S. Adve <vadve@cs.uiuc.edu>
Sat, 24 Aug 2002 14:44:23 +0000 (14:44 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3496 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/CBackend/CBackend.cpp
lib/Target/CBackend/Writer.cpp

index e33622f71f3e3a533bec835c48f40a7a60343593..f4fb7a245857961962d1753ec9abace6562279ea 100644 (file)
@@ -210,7 +210,8 @@ ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
 
   case Type::PointerTyID: {
     const PointerType *PTy = cast<PointerType>(Ty);
-    return printType(PTy->getElementType(), "(*" + NameSoFar + ")");
+    std::string ptrName = NameSoFar.length()? "(*"+NameSoFar+")" : string("*");
+    return printType(PTy->getElementType(), ptrName);
   }
 
   case Type::ArrayTyID: {
@@ -844,16 +845,20 @@ void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
       Out << (HasImplicitAddress ? "." : "->");
       Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
       I += 2;
-    } else {  // Performing array indexing. Just skip the 0
+    } else {  // First array index of 0: Just skip it
       ++I;
     }
   }
 
   for (; I != E; ++I)
     if ((*I)->getType() == Type::UIntTy) {
-      Out << "[";
+      Out << "[((int) (";                 // sign-extend from 32 (to 64) bits
       writeOperand(*I);
-      Out << "]";
+      Out << " * sizeof(";
+      printType(cast<PointerType>(Ptr->getType())->getElementType());
+      Out << "))) / sizeof(";
+      printType(cast<PointerType>(Ptr->getType())->getElementType());
+      Out << ")]";
     } else {
       Out << ".field" << cast<ConstantUInt>(*I)->getValue();
     }
index e33622f71f3e3a533bec835c48f40a7a60343593..f4fb7a245857961962d1753ec9abace6562279ea 100644 (file)
@@ -210,7 +210,8 @@ ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
 
   case Type::PointerTyID: {
     const PointerType *PTy = cast<PointerType>(Ty);
-    return printType(PTy->getElementType(), "(*" + NameSoFar + ")");
+    std::string ptrName = NameSoFar.length()? "(*"+NameSoFar+")" : string("*");
+    return printType(PTy->getElementType(), ptrName);
   }
 
   case Type::ArrayTyID: {
@@ -844,16 +845,20 @@ void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
       Out << (HasImplicitAddress ? "." : "->");
       Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
       I += 2;
-    } else {  // Performing array indexing. Just skip the 0
+    } else {  // First array index of 0: Just skip it
       ++I;
     }
   }
 
   for (; I != E; ++I)
     if ((*I)->getType() == Type::UIntTy) {
-      Out << "[";
+      Out << "[((int) (";                 // sign-extend from 32 (to 64) bits
       writeOperand(*I);
-      Out << "]";
+      Out << " * sizeof(";
+      printType(cast<PointerType>(Ptr->getType())->getElementType());
+      Out << "))) / sizeof(";
+      printType(cast<PointerType>(Ptr->getType())->getElementType());
+      Out << ")]";
     } else {
       Out << ".field" << cast<ConstantUInt>(*I)->getValue();
     }