Browse Source

Fix #241: type analysis causes truncation of integer literals when calculating with types smaller than int32.

pull/252/head
Daniel Grunwald 14 years ago
parent
commit
2783b02007
  1. 20
      ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs
  2. 37
      ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs
  3. 1
      ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj
  4. 6
      ICSharpCode.Decompiler/Tests/TestRunner.cs
  5. 27
      ICSharpCode.Decompiler/Tests/TypeAnalysisTests.cs

20
ICSharpCode.Decompiler/ILAst/InitializerPeepholeTransforms.cs

@ -148,7 +148,6 @@ namespace ICSharpCode.Decompiler.ILAst @@ -148,7 +148,6 @@ namespace ICSharpCode.Decompiler.ILAst
{
switch (elementType) {
case TypeCode.Boolean:
case TypeCode.SByte:
case TypeCode.Byte:
if (initialValue.Length == output.Length) {
for (int j = 0; j < output.Length; j++) {
@ -157,9 +156,15 @@ namespace ICSharpCode.Decompiler.ILAst @@ -157,9 +156,15 @@ namespace ICSharpCode.Decompiler.ILAst
return true;
}
return false;
case TypeCode.Char:
case TypeCode.SByte:
if (initialValue.Length == output.Length) {
for (int j = 0; j < output.Length; j++) {
output[j] = new ILExpression(ILCode.Ldc_I4, (int)unchecked((sbyte)initialValue[j]));
}
return true;
}
return false;
case TypeCode.Int16:
case TypeCode.UInt16:
if (initialValue.Length == output.Length * 2) {
for (int j = 0; j < output.Length; j++) {
output[j] = new ILExpression(ILCode.Ldc_I4, (int)BitConverter.ToInt16(initialValue, j * 2));
@ -167,6 +172,15 @@ namespace ICSharpCode.Decompiler.ILAst @@ -167,6 +172,15 @@ namespace ICSharpCode.Decompiler.ILAst
return true;
}
return false;
case TypeCode.Char:
case TypeCode.UInt16:
if (initialValue.Length == output.Length * 2) {
for (int j = 0; j < output.Length; j++) {
output[j] = new ILExpression(ILCode.Ldc_I4, (int)BitConverter.ToUInt16(initialValue, j * 2));
}
return true;
}
return false;
case TypeCode.Int32:
case TypeCode.UInt32:
if (initialValue.Length == output.Length * 4) {

37
ICSharpCode.Decompiler/ILAst/TypeAnalysis.cs

@ -543,9 +543,19 @@ namespace ICSharpCode.Decompiler.ILAst @@ -543,9 +543,19 @@ namespace ICSharpCode.Decompiler.ILAst
case ILCode.Ldc_I4:
if (IsBoolean(expectedType) && ((int)expr.Operand == 0 || (int)expr.Operand == 1))
return typeSystem.Boolean;
return (IsIntegerOrEnum(expectedType) || expectedType is PointerType) ? expectedType : typeSystem.Int32;
if (expectedType is PointerType && (int)expr.Operand == 0)
return expectedType;
if (IsIntegerOrEnum(expectedType) && OperandFitsInType(expectedType, (int)expr.Operand))
return expectedType;
else
return typeSystem.Int32;
case ILCode.Ldc_I8:
return (IsIntegerOrEnum(expectedType) || expectedType is PointerType) ? expectedType : typeSystem.Int64;
if (expectedType is PointerType && (long)expr.Operand == 0)
return expectedType;
if (IsIntegerOrEnum(expectedType) && GetInformationAmount(expectedType) >= NativeInt)
return expectedType;
else
return typeSystem.Int64;
case ILCode.Ldc_R4:
return typeSystem.Single;
case ILCode.Ldc_R8:
@ -1043,6 +1053,29 @@ namespace ICSharpCode.Decompiler.ILAst @@ -1043,6 +1053,29 @@ namespace ICSharpCode.Decompiler.ILAst
}
}
static bool OperandFitsInType(TypeReference type, int num)
{
TypeDefinition typeDef = type.Resolve() as TypeDefinition;
if (typeDef != null && typeDef.IsEnum) {
type = typeDef.Fields.Single(f => f.IsRuntimeSpecialName && !f.IsStatic).FieldType;
}
switch (type.MetadataType) {
case MetadataType.SByte:
return sbyte.MinValue <= num && num <= sbyte.MaxValue;
case MetadataType.Int16:
return short.MinValue <= num && num <= short.MaxValue;
case MetadataType.Byte:
return byte.MinValue <= num && num <= byte.MaxValue;
case MetadataType.Char:
return char.MinValue <= num && num <= char.MaxValue;
case MetadataType.UInt16:
return ushort.MinValue <= num && num <= ushort.MaxValue;
break;
default:
return true;
}
}
static bool IsArrayPointerOrReference(TypeReference type)
{
TypeSpecification typeSpec = type as TypeSpecification;

1
ICSharpCode.Decompiler/Tests/ICSharpCode.Decompiler.Tests.csproj

@ -63,6 +63,7 @@ @@ -63,6 +63,7 @@
<Compile Include="PInvoke.cs" />
<Compile Include="QueryExpressions.cs" />
<Compile Include="Switch.cs" />
<Compile Include="TypeAnalysisTests.cs" />
<Compile Include="UndocumentedExpressions.cs" />
<Compile Include="UnsafeCode.cs" />
<Compile Include="Types\S_TypeDeclarations.cs" />

6
ICSharpCode.Decompiler/Tests/TestRunner.cs

@ -142,6 +142,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -142,6 +142,12 @@ namespace ICSharpCode.Decompiler.Tests
TestFile(@"..\..\Tests\YieldReturn.cs");
}
[Test]
public void TypeAnalysis()
{
TestFile(@"..\..\Tests\TypeAnalysisTests.cs");
}
static void TestFile(string fileName)
{
string code = File.ReadAllText(fileName);

27
ICSharpCode.Decompiler/Tests/TypeAnalysisTests.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
public class TypeAnalysisTests
{
public byte SubtractFrom256(byte b)
{
return (byte)(256 - (int)b);
}
}
Loading…
Cancel
Save