Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1038 from iskiselev/Unsigned_Comparison_1037
Browse files Browse the repository at this point in the history
Support for unsigned comparison
  • Loading branch information
kg authored Feb 25, 2017
2 parents 2751b07 + 8898c06 commit d1914fa
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions JSIL/ILBlockTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,22 @@ protected JSExpression Translate_BinaryOp (ILExpression node, JSBinaryOperator o
var leftType = lhs.GetActualType(TypeSystem);
var rightType = rhs.GetActualType(TypeSystem);

if (node.Code.GetName().Contains(".un"))
{
// We need emulate unsigned comaprison
var leftTypeUnsigned = TypeUtil.GetUnsignedType(leftType, TypeSystem);
if (leftType != leftTypeUnsigned)
{
lhs = JSCastExpression.New(lhs, leftTypeUnsigned, TypeSystem, isCoercion: true);
}

var rightTypeUnsigned = TypeUtil.GetUnsignedType(rightType, TypeSystem);
if (rightType != rightTypeUnsigned)
{
rhs = JSCastExpression.New(rhs, rightTypeUnsigned, TypeSystem, isCoercion: true);
}
}

if (
TypeUtil.IsIntegral(leftType) &&
TypeUtil.IsIntegral(rightType) &&
Expand Down
34 changes: 34 additions & 0 deletions JSIL/TypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,40 @@ public static bool IsNumeric (TypeReference type) {
}
}

public static TypeReference GetUnsignedType(TypeReference type, TypeSystem typeSystem)
{
type = DereferenceType(type);

switch (type.MetadataType)
{
case MetadataType.UIntPtr:
case MetadataType.IntPtr:
return typeSystem.UIntPtr;

case MetadataType.SByte:
case MetadataType.Byte:
return typeSystem.Byte;

case MetadataType.Int16:
case MetadataType.UInt16:
return typeSystem.UInt16;

case MetadataType.Int32:
case MetadataType.UInt32:
return typeSystem.UInt32;

case MetadataType.Int64:
case MetadataType.UInt64:
return typeSystem.UInt64;

case MetadataType.Char:
return typeSystem.Char;

default:
return type;
}
}

public static bool? IsSigned (TypeReference type) {
type = DereferenceType(type);

Expand Down
27 changes: 27 additions & 0 deletions Tests/SimpleTestCases/ble.un.il
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.assembly Hello {}
.assembly extern mscorlib {}

.class public abstract sealed Program
extends [mscorlib]System.Object
{
.method static void Main()
{
.entrypoint
.maxstack 1

ldc.i4 -1
ldc.i4.5
ble.un.s END
ldstr "int32"
call void [mscorlib]System.Console::WriteLine(string)

ldc.i8 -1
ldc.i8 5
ble.un.s END
ldstr "int64"
call void [mscorlib]System.Console::WriteLine(string)

END:
ret
}
}
1 change: 1 addition & 0 deletions Tests/SimpleTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="SimpleTestCases\ble.un.il" />
<None Include="SimpleTestCases\Issue538.cs" />
<None Include="SimpleTestCases\Issue567.cs" />
<None Include="SimpleTestCasesForStubbedBcl\Issue585.cs" />
Expand Down

0 comments on commit d1914fa

Please sign in to comment.