Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Wasm: fix more occurrences of loading Sbytes into int32 without sign extending. #7764

Merged
merged 13 commits into from Nov 8, 2019
Merged
Expand Up @@ -2510,8 +2510,8 @@ private void ImportBranch(ILOpcode opcode, BasicBlock target, BasicBlock fallthr
kind = op2.Kind;
}

LLVMValueRef right = op1.ValueForStackKind(kind, _builder, false);
LLVMValueRef left = op2.ValueForStackKind(kind, _builder, false);
LLVMValueRef right = op1.ValueForStackKind(kind, _builder, TypeNeedsSignExtension(op1.Type));
LLVMValueRef left = op2.ValueForStackKind(kind, _builder, TypeNeedsSignExtension(op2.Type));

if (kind != StackValueKind.Float)
{
Expand Down Expand Up @@ -2694,8 +2694,8 @@ private void ImportBinaryOperation(ILOpcode opcode)
}

LLVMValueRef result;
LLVMValueRef left = op2.ValueForStackKind(kind, _builder, false);
LLVMValueRef right = op1.ValueForStackKind(kind, _builder, false);
LLVMValueRef left = op2.ValueForStackKind(kind, _builder, TypeNeedsSignExtension(op2.Type));
LLVMValueRef right = op1.ValueForStackKind(kind, _builder, TypeNeedsSignExtension(op1.Type));
if (kind == StackValueKind.Float)
{
if(op1.Type.IsWellKnownType(WellKnownType.Double) && op2.Type.IsWellKnownType(WellKnownType.Single))
Expand Down Expand Up @@ -2827,7 +2827,7 @@ private void ImportShiftOperation(ILOpcode opcode)
StackEntry numBitsToShift = _stack.Pop();
StackEntry valueToShift = _stack.Pop();

LLVMValueRef valueToShiftValue = valueToShift.ValueForStackKind(valueToShift.Kind, _builder, false);
LLVMValueRef valueToShiftValue = valueToShift.ValueForStackKind(valueToShift.Kind, _builder, TypeNeedsSignExtension(valueToShift.Type));

// while it seems excessive that the bits to shift should need to be 64 bits, the LLVM docs say that both operands must be the same type and a compilation failure results if this is not the case.
LLVMValueRef rhs;
Expand Down
26 changes: 25 additions & 1 deletion tests/src/Simple/HelloWasm/ILHelpers.il
Expand Up @@ -112,4 +112,28 @@
ldc.i4 0
ret
}
}

.method public static bool BneSbyteExtend () cil managed
{
// Method begins at RVA 0x2050
// Code size 8 (0x8)
.maxstack 8

.locals init (
[0] int8
)

ldc.i4.m1
stloc.0
ldloc.0
ldc.i4.m1
bne.un.s failure

ldc.i4.1
ret

failure:
ldc.i4.0
ret
}
}
25 changes: 25 additions & 0 deletions tests/src/Simple/HelloWasm/Program.cs
Expand Up @@ -1007,6 +1007,31 @@ private static void TestSByteExtend()
{
FailTest("Expected -1 and 1 but got " + x.ToString() + " and " + x2.ToString());
}

StartTest("SByte left shift");
x = (int)(s << 1);
if(x == -2)
{
PassTest();
}
else
{
FailTest("Expected -2 but got " + x.ToString());
}

sbyte minus1 = -1;
StartTest("Negative SByte op");
if((s & minus1) == -1)
{
PassTest();
}
else
{
FailTest();
}

StartTest("Negative SByte br");
EndTest(ILHelpers.ILHelpersTest.BneSbyteExtend());
}

[DllImport("*")]
Expand Down