diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index ce44f3ce308..9e10d5bc782 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -1029,6 +1029,16 @@ void CWriter::visitPHINode(PHINode &I) { void CWriter::visitBinaryOperator(Instruction &I) { // binary instructions, shift instructions, setCond instructions. assert(!isa(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) { diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index ce44f3ce308..9e10d5bc782 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -1029,6 +1029,16 @@ void CWriter::visitPHINode(PHINode &I) { void CWriter::visitBinaryOperator(Instruction &I) { // binary instructions, shift instructions, setCond instructions. assert(!isa(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) {