diff --git a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs index 1ac1869c3d8..1e1c2047079 100644 --- a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs +++ b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs @@ -395,6 +395,10 @@ private static LLVMValueRef CastIntValue(LLVMBuilderRef builder, LLVMValueRef va { return LLVM.BuildSExtOrBitCast(builder, value, type, "SExtOrBitCast"); } + else if (type.GetIntTypeWidth() > LLVM.TypeOf(value).GetIntTypeWidth()) + { + return LLVM.BuildZExtOrBitCast(builder, value, type, "ZExtOrBitCast"); + } else { Debug.Assert(typeKind == LLVMTypeKind.LLVMIntegerTypeKind); @@ -1569,7 +1573,7 @@ private void ImportLoadIndirect(TypeDesc type) LLVMValueRef pointerElementType = pointer.ValueAsType(type.MakePointerType(), _builder); _stack.Push(new LoadExpressionEntry(type != null ? GetStackValueKind(type) : StackValueKind.ByRef, "ldind", - pointerElementType, type.MakePointerType())); + pointerElementType, type)); } private void ImportStoreIndirect(int token) diff --git a/tests/src/Simple/HelloWasm/Program.cs b/tests/src/Simple/HelloWasm/Program.cs index bb8962c1294..612ce74997d 100644 --- a/tests/src/Simple/HelloWasm/Program.cs +++ b/tests/src/Simple/HelloWasm/Program.cs @@ -205,6 +205,8 @@ private static unsafe void Main(string[] args) { PrintLine("Type casting with isinst & castclass to array test: Ok."); } + + ldindTest(); PrintLine("Done"); } @@ -305,6 +307,35 @@ private static void IntToStringTest() PrintLine(intString); } + private unsafe static void ldindTest() + { + var ldindTarget = new TwoByteStr { first = byte.MaxValue, second = byte.MinValue }; + var ldindField = &ldindTarget.first; + if((*ldindField) == byte.MaxValue) + { + ldindTarget.second = byte.MaxValue; + *ldindField = byte.MinValue; + //ensure there isnt any overwrite of nearby fields + if(ldindTarget.first == byte.MinValue && ldindTarget.second == byte.MaxValue) + { + PrintLine("ldind test: Ok."); + } + else if(ldindTarget.first != byte.MinValue) + { + PrintLine("ldind test: Failed didnt update target."); + } + else + { + PrintLine("ldind test: Failed overwrote data"); + } + } + else + { + uint ldindFieldValue = *ldindField; + PrintLine("ldind test: Failed." + ldindFieldValue.ToString()); + } + } + [DllImport("*")] private static unsafe extern int printf(byte* str, byte* unused); }