Exemple #1
0
    public void Run(int InputValue)
    {
        this.InputValue = InputValue;
        keepLooping     = true;
        while (iter < MaxLength && keepLooping)
        {
            if (iter + 2 >= MaxLength)
            {
                break;
            }
            var opInstruction = new OpInstruction(Memory[iter]);
            var opCode        = opInstruction.OpCode;
            System.Console.WriteLine($"opInstruction: {Memory[iter]}");
            System.Console.WriteLine($"opCode: {opCode}");
            switch (opCode)
            {
            // misschien zouden ints toch mooier zijn
            case (int)OpCode.Adds:
                CalcOpCode1(opInstruction);
                break;

            case (int)OpCode.Multiplies:
                CalcOpCode2(opInstruction);
                break;

            case (int)OpCode.Input:
                CalcOpCode3(opInstruction);
                break;

            case (int)OpCode.Output:
                CalcOpCode4(opInstruction);
                // experimenteel:
                IsHalted    = true;
                keepLooping = false;
                break;

            case (int)OpCode.JumpIfTrue:
                CalcOpCode5(opInstruction);
                break;

            case (int)OpCode.JumpIfFalse:
                CalcOpCode6(opInstruction);
                break;

            case (int)OpCode.LessThan:
                CalcOpCode7(opInstruction);
                break;

            case (int)OpCode.Equals:
                CalcOpCode8(opInstruction);
                break;

            case (int)OpCode.Halts:
                System.Console.WriteLine("WE ZIJN BIJ 99");
                IsHalted    = true;
                keepLooping = false;
                break;
            }
        }
    }
Exemple #2
0
    private void CalcOpCode4(OpInstruction opInstruction)
    {
        var position1 = GetLocation(opInstruction.OpMode1, 1);

        OutputValue = Int32.Parse(Memory[position1]);
        iter       += 2;
    }
Exemple #3
0
    private void CalcOpCode3(OpInstruction opInstruction)
    {
        var position1 = GetLocation(opInstruction.OpMode1, 1);

        Memory[position1] = InputValue.ToString("00000");
        iter += 2;
    }
Exemple #4
0
    private void CalcOpCode8(OpInstruction opInstruction)
    {
        var value1    = Int32.Parse(Memory[GetLocation(opInstruction.OpMode1, 1)]);
        var value2    = Int32.Parse(Memory[GetLocation(opInstruction.OpMode2, 2)]);
        var position3 = Int32.Parse(Memory[GetLocation(opInstruction.OpMode3, 3)]);

        Memory[position3] = value1 == value2 ? "00001" : "00000";
        iter += 4;
    }
Exemple #5
0
    private void CalcOpCode2(OpInstruction opInstruction)
    {
        var value1    = Int32.Parse(Memory[GetLocation(opInstruction.OpMode1, 1)]);
        var value2    = Int32.Parse(Memory[GetLocation(opInstruction.OpMode2, 2)]);
        var position3 = Int32.Parse(Memory[GetLocation(opInstruction.OpMode3, 3)]);

        Memory[position3] = (value1 * value2).ToString("00000");
        iter += 4;
    }
 private int GetTargetPosition(OpInstruction i)
 {
     if (i.Data is byte)
     {
         return(1 + i.Position + i.Size + (sbyte)(byte)i.Data);
     }
     else
     {
         return(1 + i.Position + i.Size + (int)i.Data);
     }
 }
        public static int GetPushCount(CilMethod method, OpInstruction instruction)
        {
            switch (instruction.OpCode.StackBehaviourPush)
            {
            case StackBehaviour.Push0:
                return(0);

            case StackBehaviour.Push1:
            case StackBehaviour.Pushi:
            case StackBehaviour.Pushi8:
            case StackBehaviour.Pushr4:
            case StackBehaviour.Pushr8:
            case StackBehaviour.Pushref:
                return(1);

            case StackBehaviour.Varpush:
                if (instruction.OpCode.Name.StartsWith("call"))
                {
                    if (instruction.Data is ConstructorInfo)
                    {
                        return(0);
                    }

                    var mi = (MethodInfo)instruction.Data;

                    if (mi.ReturnType.FullName == "System.Void")
                    {
                        return(0);
                    }
                    else
                    {
                        return(1);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }

            case StackBehaviour.Push1_push1:
                if (instruction.OpCode.Name == "dup")
                {
                    return(2);
                }
                else
                {
                    throw new NotImplementedException();
                }

            default:
                throw new NotImplementedException();
            }
        }
Exemple #8
0
    private void CalcOpCode6(OpInstruction opInstruction)
    {
        var position1 = Int32.Parse(Memory[GetLocation(opInstruction.OpMode1, 1)]);

        if (Int32.Parse(Memory[position1]) == 0)
        {
            iter = Int32.Parse(Memory[GetLocation(opInstruction.OpMode2, 2)]);
        }
        else
        {
            iter += 3;
        }
    }
        public static int?GetPopCount(CilMethod method, OpInstruction instruction)
        {
            if (instruction.OpCode.Name.StartsWith("leave"))
            {
                return(null); // pops all
            }
            switch (instruction.OpCode.StackBehaviourPop)
            {
            case StackBehaviour.Pop0:
                return(0);

            case StackBehaviour.Pop1:
            case StackBehaviour.Popi:
            case StackBehaviour.Popref:
                return(1);

            case StackBehaviour.Pop1_pop1:
            case StackBehaviour.Popi_pop1:
            case StackBehaviour.Popi_popi:
            case StackBehaviour.Popi_popi8:
            case StackBehaviour.Popi_popr4:
            case StackBehaviour.Popi_popr8:
            case StackBehaviour.Popref_pop1:
            case StackBehaviour.Popref_popi:
                return(2);

            case StackBehaviour.Popi_popi_popi:
            case StackBehaviour.Popref_popi_pop1:
            case StackBehaviour.Popref_popi_popi:
            case StackBehaviour.Popref_popi_popi8:
            case StackBehaviour.Popref_popi_popr4:
            case StackBehaviour.Popref_popi_popr8:
            case StackBehaviour.Popref_popi_popref:
                return(3);

            case StackBehaviour.Varpop:
                if (instruction.OpCode.Name.StartsWith("call"))
                {
                    var callTarget = (MethodBase)instruction.Data;

                    var result = callTarget.GetParameters().Count();

                    if (callTarget.IsStatic == false)
                    {
                        result++;
                    }

                    return(result);
                }
                else if (instruction.OpCode.Name == "newobj")
                {
                    return(((ConstructorInfo)instruction.Data).GetParameters().Count());
                }
                else if (instruction.OpCode.Name == "ret")
                {
                    var mi = method.ReflectionMethod as MethodInfo;

                    if (mi != null && mi.ReturnType.FullName != "System.Void")
                    {
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }

            default:
                throw new NotImplementedException();
            }
        }