public static MethodInfo Compile_LdNull() { var method = new ILMethod(nameof(ILOpCodeValues.Ldnull)); method.AddInstructions(ILInstruction.Create(OpCodes.Ldnull), ILInstruction.Ret); return(method.Compile()); }
public void CreateFromOpCodeTest() { var noOp = ILInstruction.Create(OpCodes.Nop); Assert.IsNotNull(noOp); Assert.IsTrue(noOp.OpCode == OpCodes.Nop); Assert.IsNull(noOp.Arg); }
public void CreateFromILOpCodeValuesWithArgTest() { var arg = 0; var ldarga_S = ILInstruction.Create(ILOpCodeValues.Ldarga_S, arg); Assert.IsNotNull(ldarga_S); Assert.IsTrue(ldarga_S.OpCode == OpCodes.Ldarga_S); Assert.IsNotNull(ldarga_S.Arg); Assert.IsTrue((int)ldarga_S.Arg == arg); }
public void BuildILInstructionsTest() { var opCode1 = OpCodes.Ret; var instruction = ILInstruction.Create(opCode1); var instructions = (new[] { instruction }).ToList(); var frame = ILStackFrameBuilder.Build(instructions); Assert.IsTrue(frame.Stream.Count == 1); Assert.IsTrue(frame.Stream[0].OpCode == opCode1); }
public static MethodInfo Compile_Add_Ovf() { var method = ILMethodBuilder <int> .Create(nameof(ILOpCodeValues.Add_Ovf)); method.AddInstructions(ILInstruction.Create(OpCodes.Ldc_I4, int.MaxValue), ILInstruction.Create(OpCodes.Ldc_I4, int.MaxValue), ILInstruction.Create(OpCodes.Add_Ovf), ILInstruction.Ret); return(method.Compile()); }
public void WriteILInstructionTest() { var opCode = OpCodes.Nop; var instruction = ILInstruction.Create(opCode); var builder = new ILInstructionBuilder(); builder.Write(instruction); Assert.IsTrue(builder.Instructions.Count == 1); Assert.IsTrue(builder.Instructions[0].OpCode == opCode); Assert.IsNull(builder.Instructions[0].Arg); }
public static MethodInfo Compile_Ldloc_0() { var method = new ILMethod(nameof(ILOpCodeValues.Ldloc_0), typeof(int)); method.AddLocals(typeof(int)); method.AddInstructions( ILInstruction.Create(OpCodes.Ldc_I4_1), ILInstruction.Create(OpCodes.Stloc_0), ILInstruction.Create(OpCodes.Ldloc_0), ILInstruction.Ret); return(method.Compile()); }
public void WriteILInstructionsTest() { var opCode1 = OpCodes.Nop; var opCode2 = OpCodes.Ret; var instruction1 = ILInstruction.Create(opCode1); var instruction2 = ILInstruction.Create(opCode2); var builder = new ILInstructionBuilder(); builder.Write(instruction1, instruction2); Assert.IsTrue(builder.Instructions.Count == 2); Assert.IsTrue(builder.Instructions[0].OpCode == opCode1); Assert.IsNull(builder.Instructions[0].Arg); Assert.IsTrue(builder.Instructions[1].OpCode == opCode2); Assert.IsNull(builder.Instructions[1].Arg); }
public void ToStringTest() { var instruction1 = ILInstruction.Create(OpCodes.Nop); Assert.IsNotNull(instruction1); var expected1 = $"IL_0000 nop"; Assert.IsTrue(instruction1.ToString() == expected1, $"Actual: {instruction1.ToString()}\r\nExpected:{expected1}"); var instruction2 = ILInstruction.Create(OpCodes.Ldarga_S, 1); Assert.IsNotNull(instruction2); var expected2 = $"IL_0000 ldarga.s 1"; Assert.IsTrue(instruction2.ToString() == expected2, $"Actual: {instruction2.ToString()}\r\nExpected:{expected2}"); }
public void TestReadInlineBrTarget() { var builder = new ILInstructionBuilder(); builder.Write(ILInstruction.Create(ILOpCodeValues.Br, 0)); builder.Write(ILOpCodeValues.Ldc_I8, 1L); builder.Write(ILOpCodeValues.Ret); var ilMethod = new Models.ILMethod(MethodBase.GetCurrentMethod().Name, typeof(long)); ilMethod.AddInstructions(builder.Instructions.ToArray()); var method = ilMethod.Compile(); var expected = method.Invoke(null, Type.EmptyTypes); var instructions = ILInstructionReader.FromMethod(method); ExecuteAndAssertResult(instructions, expected, OperandType.InlineBrTarget); }
public void BuildAndExecuteILInstructionsTest() { var opCode1 = OpCodes.Ldc_I4_1; var opCode2 = OpCodes.Ret; var instruction1 = ILInstruction.Create(opCode1); var instruction2 = ILInstruction.Create(opCode2); var instructions = (new[] { instruction1, instruction2 }).ToList(); var frame = ILStackFrameBuilder.BuildAndExecute(instructions); Assert.IsTrue(frame.Stream.Count == 2); Assert.IsTrue(frame.Stream[0].OpCode == opCode1); Assert.IsTrue(frame.Stream[1].OpCode == opCode2); var actual = frame.ReturnResult; Assert.IsNotNull(actual); var expected = 1; Assert.IsTrue((int)actual == expected, $"Actual:{actual}\r\nExpected:{expected}\r\n"); }
public void AddInstructions(params ILOpCodeValues[] instructions) { Instructions.AddRange(instructions.Select(x => ILInstruction.Create(x))); }
public void CreateFromILOpCodeValuesFailsWhenNotSpecifyingRequiredArgumentTest() { var noOp = ILInstruction.Create(ILOpCodeValues.Ldarga_S); }
public void CreateFromILOpCodeValuesFailsWhenSpecifyingArgumentTest() { var noOp = ILInstruction.Create(ILOpCodeValues.Nop, 0); }
public void TestEmitSwitch() { var endSwitchInstruction = ILInstruction.Create(ILOpCodeValues.Nop); endSwitchInstruction.Label = 2; var addInstructions = new[] { ILInstruction.Create(ILOpCodeValues.Add), ILInstruction.Create(ILOpCodeValues.Br_S, endSwitchInstruction) }; var subInstructions = new[] { ILInstruction.Create(ILOpCodeValues.Sub), ILInstruction.Create(ILOpCodeValues.Br_S, endSwitchInstruction) }; addInstructions[0].Label = 0; subInstructions[0].Label = 1; var exceptionType = typeof(ArgumentOutOfRangeException); var ctor = exceptionType.GetConstructor(Type.EmptyTypes); var defaultInstuctions = new[] { ILInstruction.Create(ILOpCodeValues.Newobj, ctor.MetadataToken), ILInstruction.Create(ILOpCodeValues.Throw) }; var switchInstuction = ILInstruction.Create(ILOpCodeValues.Switch, new[] { addInstructions[0], subInstructions[0] }); var builder = new ILInstructionBuilder(); //var b= arg[b]; builder.Write(ILOpCodeValues.Ldarg_1); //var a= arg[1]; builder.Write(ILOpCodeValues.Ldarg_2); //switch(arg[0]) builder.Write(ILOpCodeValues.Ldarg_0); builder.Write(switchInstuction); //case default builder.Write(defaultInstuctions); //case 0: add builder.Write(addInstructions); //case 1: sub builder.Write(subInstructions); builder.Write(endSwitchInstruction); builder.Write(ILOpCodeValues.Ret); var frame = ILStackFrameBuilder.Build(builder.Instructions); frame.Args = new object[] { 0, 1, 2 }; frame.Execute(); var expected = 3; Assert.IsNull(frame.Exception, $"Executing switch: add throw an exception {frame?.Exception}"); Assert.IsTrue(frame.Stack.Count == 0, "Stack was not cleared executing switch: add"); Assert.IsTrue((int)frame.ReturnResult == expected, $"Actual: {frame.ReturnResult}\r\nExpected: {expected}"); expected = -1; frame.Args = new object[] { 1, 1, 2 }; frame.Execute(); Assert.IsNull(frame.Exception, $"Executing switch: add throw an exception {frame?.Exception}"); Assert.IsTrue(frame.Stack.Count == 0, "Stack was not cleared executing switch: add"); Assert.IsTrue((int)frame.ReturnResult == expected, $"Actual: {frame.ReturnResult}\r\nExpected: {expected}"); frame.Args = new object[] { 2, 1, 2 }; frame.Execute(); Assert.IsNotNull(frame.Exception, $"Executing switch failed to execute default case to and throw and exception."); Assert.IsInstanceOfType(frame.Exception, typeof(ArgumentOutOfRangeException), $"Frame failed to throw {nameof(ArgumentOutOfRangeException)}"); Assert.IsNull(frame.ReturnResult, $"Actual: {frame.ReturnResult}\r\nExpected: [null]"); //var type = BuildSwitchTestType(); //var switchMethod = type.GetMethod("SwitchTest"); //Assert.IsNotNull(switchMethod); //var instructions = ILInstructionReader.FromMethod(switchMethod); var ilMethod = new ILMethod(MethodBase.GetCurrentMethod().Name, expected.GetType()); ilMethod.AddParameters(new[] { typeof(int), typeof(int), typeof(int) }); ilMethod.AddInstructions(builder.Instructions.ToArray()); ilMethod.Module = exceptionType.Module; var method = ilMethod.Compile(); var actual = method.Invoke(null, new object[] { 0, 1, 2 }); expected = 3; Assert.IsTrue((int)actual == expected, $"Actual: {actual}\r\nExpected:{expected}"); actual = method.Invoke(null, new object[] { 1, 1, 2 }); expected = -1; Assert.IsTrue((int)actual == expected, $"Actual: {actual}\r\nExpected:{expected}"); Exception exception = null; try { actual = method.Invoke(null, new object[] { 2, 1, 2 }); } catch (TargetInvocationException ex) { exception = ex.InnerException; } Assert.IsNotNull(exception, $"Failed to catch argument exception"); Assert.IsInstanceOfType(exception, exceptionType); }