From: Stephane Delcroix Date: Mon, 12 Dec 2016 09:57:01 +0000 (+0100) Subject: [XamlC] fix loading ulongs, optimize bytecode for ulongs (#611) X-Git-Tag: 2.3.3.175~21 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=22bdca32339838702920802e49465264fcccd48b;p=platform%2Fupstream%2Fxamarin-forms.git [XamlC] fix loading ulongs, optimize bytecode for ulongs (#611) --- diff --git a/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs b/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs index 0c9b9e6..ec06879 100644 --- a/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs +++ b/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs @@ -128,11 +128,11 @@ namespace Xamarin.Forms.Build.Tasks else if (targetTypeRef.FullName == "System.Byte") yield return Instruction.Create(OpCodes.Ldc_I4, Byte.Parse(str, CultureInfo.InvariantCulture)); else if (targetTypeRef.FullName == "System.UInt16") - yield return Instruction.Create(OpCodes.Ldc_I4, UInt16.Parse(str, CultureInfo.InvariantCulture)); + yield return Instruction.Create(OpCodes.Ldc_I4, unchecked((int)UInt16.Parse(str, CultureInfo.InvariantCulture))); else if (targetTypeRef.FullName == "System.UInt32") yield return Instruction.Create(OpCodes.Ldc_I4, UInt32.Parse(str, CultureInfo.InvariantCulture)); else if (targetTypeRef.FullName == "System.UInt64") - yield return Instruction.Create(OpCodes.Ldc_I8, UInt64.Parse(str, CultureInfo.InvariantCulture)); + yield return Instruction.Create(OpCodes.Ldc_I8, unchecked((long)UInt64.Parse(str, CultureInfo.InvariantCulture))); else if (targetTypeRef.FullName == "System.Single") yield return Instruction.Create(OpCodes.Ldc_R4, Single.Parse(str, CultureInfo.InvariantCulture)); else if (targetTypeRef.FullName == "System.Double") diff --git a/Xamarin.Forms.Build.Tasks/XamlCTask.cs b/Xamarin.Forms.Build.Tasks/XamlCTask.cs index eda44a9..5127efd 100644 --- a/Xamarin.Forms.Build.Tasks/XamlCTask.cs +++ b/Xamarin.Forms.Build.Tasks/XamlCTask.cs @@ -185,6 +185,7 @@ namespace Xamarin.Forms.Build.Tasks if (OptimizeIL) { Logger.LogString(2, " Optimizing IL... "); + OptimizeLongs(initComp.Body); initComp.Body.OptimizeMacros(); Logger.LogLine(2, "done"); } @@ -236,6 +237,28 @@ namespace Xamarin.Forms.Build.Tasks return success; } + static void ExpandMacro(Instruction instruction, OpCode opcode, object operand) + { + instruction.OpCode = opcode; + instruction.Operand = operand; + } + + //this can be removed if/when https://github.com/jbevain/cecil/pull/307 is released in a nuget we consume + static void OptimizeLongs(MethodBody self) + { + var method = self.Method; + for (var i = 0; i < self.Instructions.Count; i++) { + var instruction = self.Instructions[i]; + if (instruction.OpCode.Code != Code.Ldc_I8) + continue; + var l = (long)instruction.Operand; + if (l < int.MinValue || l > int.MaxValue) + continue; + ExpandMacro(instruction, OpCodes.Ldc_I4, unchecked((int)l)); + self.Instructions.Insert(++i, Instruction.Create(OpCodes.Conv_I8)); + } + } + bool TryCoreCompile(MethodDefinition initComp, MethodDefinition initCompRuntime, ILRootNode rootnode, out Exception exception) { try { diff --git a/Xamarin.Forms.Xaml.UnitTests/I8.xaml b/Xamarin.Forms.Xaml.UnitTests/I8.xaml new file mode 100644 index 0000000..09f2712 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/I8.xaml @@ -0,0 +1,22 @@ + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/I8.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/I8.xaml.cs new file mode 100644 index 0000000..4d7d874 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/I8.xaml.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class I8 : ContentPage + { + public long l0 { get; set; } + public long l1 { get; set; } + public long l2 { get; set; } + public long l3 { get; set; } + public long l4 { get; set; } + public long l5 { get; set; } + public long l6 { get; set; } + public long l7 { get; set; } + public long l8 { get; set; } + public long l9 { get; set; } + public ulong ul0 { get; set; } + public ulong ul1 { get; set; } + public ulong ul2 { get; set; } + public ulong ul3 { get; set; } + public ulong ul4 { get; set; } + public ulong ul5 { get; set; } + + public I8() + { + InitializeComponent(); + } + + public I8(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + public class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + + [TestCase(false)] + [TestCase(true)] + public void I8AreConverted(bool useCompiledXaml) + { + var p = new I8(useCompiledXaml); + Assert.AreEqual(0L, p.l0); + Assert.AreEqual((long)int.MaxValue, p.l1); + Assert.AreEqual((long)uint.MaxValue, p.l2); + Assert.AreEqual(long.MaxValue, p.l3); + Assert.AreEqual((long)-int.MaxValue, p.l4); + Assert.AreEqual((long)-uint.MaxValue, p.l5); + Assert.AreEqual(-long.MaxValue, p.l6); + Assert.AreEqual((long)256, p.l7); + Assert.AreEqual((long)-256, p.l8); + Assert.AreEqual((long)127, p.l9); + Assert.AreEqual(0L, p.ul0); + Assert.AreEqual((long)int.MaxValue, p.ul1); + Assert.AreEqual((long)uint.MaxValue, p.ul2); + Assert.AreEqual(long.MaxValue, p.ul3); + Assert.AreEqual(ulong.MaxValue, p.ul4); + Assert.AreEqual((ulong)256, p.ul5); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs b/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs index ccbfa20..6e63f6a 100644 --- a/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs +++ b/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs @@ -19,6 +19,7 @@ namespace Xamarin.Forms.Xaml.UnitTests Assembly = assembly, ReferencePath = string.Join(";", refs), KeepXamlResources = true, + OptimizeIL = true, Type = type.FullName }; diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index 5ce458c..2234c7f 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -400,6 +400,9 @@ Bz46921.xaml + + I8.xaml + @@ -721,6 +724,9 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml +