From 22bdca32339838702920802e49465264fcccd48b Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Mon, 12 Dec 2016 10:57:01 +0100 Subject: [XamlC] fix loading ulongs, optimize bytecode for ulongs (#611) --- Xamarin.Forms.Build.Tasks/NodeILExtensions.cs | 4 ++-- Xamarin.Forms.Build.Tasks/XamlCTask.cs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'Xamarin.Forms.Build.Tasks') diff --git a/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs b/Xamarin.Forms.Build.Tasks/NodeILExtensions.cs index 0c9b9e6b..ec068797 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 eda44a9a..5127efda 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 { -- cgit v1.2.3