Ejemplo n.º 1
0
        public void FloatComparison(CilCode code, double a, double b, TrileanValue expected)
        {
            var stack = ExecutionContext.ProgramState.Stack;

            stack.Push(new FValue(a));
            stack.Push(new FValue(b));

            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(code.ToOpCode()));

            Assert.True(result.IsSuccess);
            Assert.Equal(expected, ((I4Value)stack.Top).IsNonZero);
        }
        public void OptimizeFirst4ArgumentsToMacros(int index, CilCode expectedMacro)
        {
            var instructions = CreateDummyMethod(false, 4, 0);

            instructions.Add(CilOpCodes.Ldarg, instructions.Owner.Owner.Parameters[index]);
            instructions.Add(CilOpCodes.Ret);

            instructions.OptimizeMacros();

            Assert.Equal(expectedMacro.ToOpCode(), instructions[0].OpCode);
            Assert.Null(instructions[0].Operand);
        }
Ejemplo n.º 3
0
        public void I4Comparison(CilCode code, int a, int b, bool expectedToTakeBranch)
        {
            var instruction    = new CilInstruction(code.ToOpCode(), new CilOffsetLabel(0x1234));
            int expectedOffset = expectedToTakeBranch ? 0x1234 : instruction.Offset + instruction.Size;

            var stack = ExecutionContext.ProgramState.Stack;

            stack.Push(new I4Value(a));
            stack.Push(new I4Value(b));

            var result = Dispatcher.Execute(ExecutionContext, instruction);

            Assert.True(result.IsSuccess);
            Assert.Equal(0, stack.Size);
            Assert.Equal(expectedOffset, ExecutionContext.ProgramState.ProgramCounter);
        }
Ejemplo n.º 4
0
        public void ConvertI4OnStackToInt64(CilCode code, int value, long?expected)
        {
            var stack = ExecutionContext.ProgramState.Stack;

            stack.Push(new I4Value(value));

            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(code.ToOpCode()));

            Assert.Equal(expected.HasValue, result.IsSuccess);

            if (expected.HasValue)
            {
                Assert.Equal(new I8Value(expected.Value), stack.Top);
            }
            else
            {
                Assert.IsAssignableFrom <OverflowException>(result.Exception);
            }
        }
Ejemplo n.º 5
0
        public void LdelemOnInt32ArrayShouldTruncateAndSignExtendWhenNecessary(CilCode code, int arrayElementValue, int expectedValue)
        {
            var environment = ExecutionContext.GetService <ICilRuntimeEnvironment>();
            var marshaller  = environment.CliMarshaller;

            var array = environment.ValueFactory.AllocateArray(environment.Module.CorLibTypeFactory.Int32, 1);

            array.StoreElementI4(0, new I4Value(arrayElementValue), marshaller);

            var stack = ExecutionContext.ProgramState.Stack;

            stack.Push(marshaller.ToCliValue(array, array.Type));
            stack.Push(new I4Value(0));

            var result = Dispatcher.Execute(ExecutionContext, new CilInstruction(code.ToOpCode()));

            Assert.True(result.IsSuccess, $"Unexpected {result.Exception?.GetType()}: {result.Exception?.Message}");
            Assert.Equal(new I4Value(expectedValue), stack.Top);
        }
Ejemplo n.º 6
0
        public void ArithmeticShouldBePure(CilCode code)
        {
            var instruction = new CilInstruction(code.ToOpCode());

            Assert.Equal(Trilean.True, _classifier.IsPure(instruction));
        }
Ejemplo n.º 7
0
        public void PushingConstantsShouldBePure(CilCode code, object operand)
        {
            var instruction = new CilInstruction(code.ToOpCode(), operand);

            Assert.Equal(Trilean.True, _classifier.IsPure(instruction));
        }
Ejemplo n.º 8
0
        public void InvalidPrimitiveOperandShouldThrow(CilCode code, object operand)
        {
            var stream    = new MemoryStream();
            var writer    = new BinaryStreamWriter(stream);
            var assembler = new CilAssembler(writer, new MockOperandBuilder());

            Assert.ThrowsAny <ArgumentException>(() =>
                                                 assembler.WriteInstruction(new CilInstruction(code.ToOpCode(), operand)));
        }