Fix 2003-06-23-PromotedExprs.llx -- if we are adding two bytes we better
authorBrian Gaeke <gaeke@uiuc.edu>
Mon, 23 Jun 2003 20:00:51 +0000 (20:00 +0000)
committerBrian Gaeke <gaeke@uiuc.edu>
Mon, 23 Jun 2003 20:00:51 +0000 (20:00 +0000)
explicitly cast the result to be a byte, or C will gleefully promote it
to int.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6869 91177308-0d34-0410-b5e6-96231b3b80d8

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

index ce44f3ce3084c696215ada5e6ad911e17ae74ac8..9e10d5bc7827b77126776b93f69437a85c7143f7 100644 (file)
@@ -1029,6 +1029,16 @@ void CWriter::visitPHINode(PHINode &I) {
 void CWriter::visitBinaryOperator(Instruction &I) {
   // binary instructions, shift instructions, setCond instructions.
   assert(!isa<PointerType>(I.getType()));
+
+  // We must cast the results of binary operations which might be promoted.
+  bool needsCast = false;
+  if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
+      || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)) {
+    needsCast = true;
+    Out << "((";
+    printType(Out, I.getType(), "", false, false);
+    Out << ")(";
+  }
       
   writeOperand(I.getOperand(0));
 
@@ -1053,6 +1063,10 @@ void CWriter::visitBinaryOperator(Instruction &I) {
   }
 
   writeOperand(I.getOperand(1));
+
+  if (needsCast) {
+    Out << "))";
+  }
 }
 
 void CWriter::visitCastInst(CastInst &I) {
index ce44f3ce3084c696215ada5e6ad911e17ae74ac8..9e10d5bc7827b77126776b93f69437a85c7143f7 100644 (file)
@@ -1029,6 +1029,16 @@ void CWriter::visitPHINode(PHINode &I) {
 void CWriter::visitBinaryOperator(Instruction &I) {
   // binary instructions, shift instructions, setCond instructions.
   assert(!isa<PointerType>(I.getType()));
+
+  // We must cast the results of binary operations which might be promoted.
+  bool needsCast = false;
+  if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
+      || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)) {
+    needsCast = true;
+    Out << "((";
+    printType(Out, I.getType(), "", false, false);
+    Out << ")(";
+  }
       
   writeOperand(I.getOperand(0));
 
@@ -1053,6 +1063,10 @@ void CWriter::visitBinaryOperator(Instruction &I) {
   }
 
   writeOperand(I.getOperand(1));
+
+  if (needsCast) {
+    Out << "))";
+  }
 }
 
 void CWriter::visitCastInst(CastInst &I) {