Exemplo n.º 1
0
        public override VMPrimitiveExitCode Execute(VMStackFrame frame, out VMInstruction instruction)
        {
            var result = Function.Execute(frame, ref frame.InstructionPointer, frame.Args);

            instruction = frame.GetCurrentInstruction();
            return(result ? VMPrimitiveExitCode.RETURN_TRUE : VMPrimitiveExitCode.RETURN_FALSE);
        }
Exemplo n.º 2
0
 public VMInstructionDisplay(VMInstruction inst)
 {
     //Routine no longer has an instance of VM.
     //this.Primitive = inst.Function.VM.Context.Primitives[inst.Opcode];
     //vm.Context.GetPrimitive(inst.Opcode)
     this.Instruction = inst;
 }
Exemplo n.º 3
0
        public override VMPrimitiveExitCode Execute(VMStackFrame frame, out VMInstruction instruction)
        {
            var result = Function.Execute(frame, ref frame.InstructionPointer);

            instruction = frame.GetCurrentInstruction();
            return(result);
        }
Exemplo n.º 4
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[1];

            buf[0] = (byte)VMOpCode.Null;
            return(buf);
        }
Exemplo n.º 5
0
        override public VMInstruction[] GetInstructions()
        {
            VMInstruction[] valueInstructions = value.GetInstructions();

            VMInstruction[] toReturn = new VMInstruction[valueInstructions.Length + 1];

            Array.Copy(valueInstructions, toReturn, valueInstructions.Length);

            InstructionType instruction = InstructionType.Negate;

            switch (op)
            {
            case "-":
                instruction = InstructionType.Negate;
                break;

            case "!":
                instruction = InstructionType.Not;
                break;
            }

            toReturn[toReturn.Length - 1] = new VMInstruction(instruction);

            return(toReturn);
        }
Exemplo n.º 6
0
        public static Instruction Stloc(VMInstruction instruction)
        {
            object num    = instruction.Operand;         //i know than is not that but i'm lazy
            Local  locals = _current.Body.Variables.Add(new Local(module.Import(num.GetType()).ToTypeSig()));

            return(new Instruction(OpCodes.Stloc, locals));
        }
Exemplo n.º 7
0
        public static Instruction Ldloc(VMInstruction instruction)
        {
            short num    = (short)instruction.Operand;
            Local locals = _current.Body.Variables[num];

            return(new Instruction(OpCodes.Ldloc, locals));
        }
Exemplo n.º 8
0
        private void HandleResult(VMStackFrame frame, VMInstruction instruction, VMPrimitiveExitCode result)
        {
            switch (result)
            {
            /** Dont advance the instruction pointer, this primitive isnt finished yet **/
            case VMPrimitiveExitCode.CONTINUE_NEXT_TICK:
                break;

            case VMPrimitiveExitCode.ERROR:
                Pop(result);
                break;

            case VMPrimitiveExitCode.RETURN_TRUE:
            case VMPrimitiveExitCode.RETURN_FALSE:
                /** pop stack and return false **/
                Pop(result);
                break;

            case VMPrimitiveExitCode.GOTO_TRUE:
                MoveToInstruction(frame, instruction.TruePointer, true);
                break;

            case VMPrimitiveExitCode.GOTO_FALSE:
                MoveToInstruction(frame, instruction.FalsePointer, true);
                break;

            case VMPrimitiveExitCode.GOTO_TRUE_NEXT_TICK:
                MoveToInstruction(frame, instruction.TruePointer, false);
                break;

            case VMPrimitiveExitCode.GOTO_FALSE_NEXT_TICK:
                MoveToInstruction(frame, instruction.FalsePointer, false);
                break;
            }
        }
Exemplo n.º 9
0
        public static Instruction Stfld(VMInstruction instruction)
        {
            Tuple <short, int, bool> tuple_Stfld = (Tuple <short, int, bool>)instruction.Operand;
            Assembly  reference_Stfld            = GetReference(tuple_Stfld.Item1);
            FieldInfo field_Stfld = reference_Stfld.ManifestModule.ResolveField(tuple_Stfld.Item2);

            return(Instruction.Create(OpCodes.Stfld, module.Import(field_Stfld)));
        }
Exemplo n.º 10
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[5];

            buf[0] = (byte)VMOpCode.Je;
            Array.Copy(BitConverter.GetBytes(helper.Get((int)instruction.Operand)), 0, buf, 1, 4);
            return(buf);
        }
Exemplo n.º 11
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[3];

            buf[0] = (byte)VMOpCode.Starg;
            Array.Copy(BitConverter.GetBytes((short)instruction.Operand), 0, buf, 1, 2);
            return(buf);
        }
Exemplo n.º 12
0
        public static Instruction NewArray(VMInstruction instruction)
        {
            Tuple <short, int, bool> tuple_Newarr = (Tuple <short, int, bool>)instruction.Operand;
            Assembly reference_Newarr             = GetReference(tuple_Newarr.Item1);
            TypeInfo elementType = reference_Newarr.ManifestModule.ResolveType(tuple_Newarr.Item2) as TypeInfo;

            return(Instruction.Create(OpCodes.Newarr, module.Import(elementType)));
        }
Exemplo n.º 13
0
        public static Instruction Call(VMInstruction instruction)
        {
            Tuple <short, int, bool> tuple_Call = (Tuple <short, int, bool>)instruction.Operand;
            Assembly   reference_Call           = GetReference(tuple_Call.Item1);
            MethodInfo method_call = reference_Call.ManifestModule.ResolveMethod(tuple_Call.Item2) as MethodInfo;

            return(Instruction.Create(OpCodes.Call, module.Import(method_call)));
        }
Exemplo n.º 14
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[9];

            buf[0] = (byte)VMOpCode.Int64;

            Array.Copy(BitConverter.GetBytes((long)instruction.Operand), 0, buf, 1, 8);
            return(buf);
        }
Exemplo n.º 15
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var str = Encoding.UTF8.GetBytes((string)instruction.Operand);
            var buf = new byte[5 + str.Length];

            buf[0] = (byte)VMOpCode.String;
            Array.Copy(BitConverter.GetBytes(str.Length), 0, buf, 1, 4);
            Array.Copy(str, 0, buf, 5, str.Length);
            return(buf);
        }
Exemplo n.º 16
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[7];

            buf[0]            = (byte)VMOpCode.Ldfld;
            var(refid, field) = (Tuple <short, FieldDef>)instruction.Operand;
            Array.Copy(BitConverter.GetBytes(refid), 0, buf, 1, 2);
            Array.Copy(BitConverter.GetBytes(TokenGetter.GetMdToken(field)), 0, buf, 3, 4);
            return(buf);
        }
Exemplo n.º 17
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[7];

            buf[0] = (byte)VMOpCode.Newarr;
            var(referenceid, type) = (Tuple <short, TypeDef>)instruction.Operand;
            Array.Copy(BitConverter.GetBytes(referenceid), 0, buf, 1, 2);
            Array.Copy(BitConverter.GetBytes(TokenGetter.GetMdToken(type)), 0, buf, 3, 4);
            return(buf);
        }
Exemplo n.º 18
0
        public static VMRoutine Assemble(VM vm, BHAV bhav)
        {
            var context = vm.Context;

            var routine = new VMRoutine();

            routine.Locals    = bhav.Locals;
            routine.Arguments = bhav.Args;
            routine.Type      = bhav.Type;
            routine.ID        = bhav.ChunkID;
            routine.Chunk     = bhav;
            routine.VM        = vm;
            routine.Rti       = new VMFunctionRTI {
                Name = bhav.ChunkLabel
            };

            VMInstruction[] instructions = new VMInstruction[bhav.Instructions.Length];
            for (var i = 0; i < bhav.Instructions.Length; i++)
            {
                var bhavInstruction = bhav.Instructions[i];
                var instruction     = new VMInstruction();

                instruction.Index        = (byte)i;
                instruction.Opcode       = bhavInstruction.Opcode;
                instruction.Operand      = null;
                instruction.FalsePointer = bhavInstruction.FalsePointer;
                instruction.TruePointer  = bhavInstruction.TruePointer;
                instruction.Breakpoint   = bhavInstruction.Breakpoint;
                instruction.Function     = routine;

                /** Routine call **/
                if (instruction.Opcode >= 256)
                {
                    var operand = new VMSubRoutineOperand();
                    operand.Read(bhavInstruction.Operand);
                    instruction.Operand = operand;
                }
                else
                {
                    var primitive = context.Primitives[instruction.Opcode];
                    if (primitive != null)
                    {
                        if (primitive.OperandModel != null)
                        {
                            VMPrimitiveOperand operand = (VMPrimitiveOperand)Activator.CreateInstance(primitive.OperandModel);
                            operand.Read(bhavInstruction.Operand);
                            instruction.Operand = operand;
                        }
                    }
                }
                instructions[i] = instruction;
            }
            routine.Instructions = instructions;
            return(routine);
        }
Exemplo n.º 19
0
        public static VMRoutine Assemble(VM vm, BHAV bhav)
        {
            var context = vm.Context;

            var routine = new VMRoutine();
            routine.Locals = bhav.Locals;
            routine.Arguments = bhav.Args;
            routine.Type = bhav.Type;
            routine.ID = bhav.ChunkID;
            routine.VM = vm;
            routine.Rti = new VMFunctionRTI {
                Name = bhav.ChunkLabel
            };

            VMInstruction[] instructions = new VMInstruction[bhav.Instructions.Length];
            for (var i = 0; i < bhav.Instructions.Length; i++)
            {
                var bhavInstruction = bhav.Instructions[i];
                var instruction = new VMInstruction();

                instruction.Index = (byte)i;
                instruction.Opcode = bhavInstruction.Opcode;
                instruction.Operand = bhavInstruction.Operand;
                instruction.FalsePointer = bhavInstruction.FalsePointer;
                instruction.TruePointer = bhavInstruction.TruePointer;
                instruction.Function = routine;

                /** Routine call **/
                if (instruction.Opcode >= 256)
                {
                    var operand = new VMSubRoutineOperand();
                    operand.Read(bhavInstruction.Operand);
                    instruction.Operand = operand;
                }
                else
                {
                    var primitive = context.GetPrimitive(instruction.Opcode);
                    if (primitive != null)
                    {
                        if (primitive.OperandModel != null)
                        {
                            VMPrimitiveOperand operand = (VMPrimitiveOperand)Activator.CreateInstance(primitive.OperandModel);
                            operand.Read(bhavInstruction.Operand);
                            instruction.Operand = operand;
                        }
                    }
                }
                instructions[i] = instruction;

            }
            routine.Instructions = instructions;
            return routine;
        }
Exemplo n.º 20
0
        override public VMInstruction[] GetInstructions()
        {
            VMInstruction[] valueInstructions = value.GetInstructions();

            VMInstruction[] toReturn = new VMInstruction[valueInstructions.Length + 1];

            Array.Copy(valueInstructions, toReturn, valueInstructions.Length);

            toReturn[toReturn.Length - 1] = new VMInstruction(InstructionType.SetLocal, localIndex);

            return(toReturn);
        }
Exemplo n.º 21
0
        public static Instruction Reconvert(VMInstruction _instruction, VMOpCode vMOpCode)
        {
            switch (vMOpCode)
            {
            case VMOpCode.Add: return(Add(_instruction));

            case VMOpCode.Call: return(Call(_instruction));

            case VMOpCode.Cgt: return(Cgt(_instruction));

            case VMOpCode.Clt: return(Clt(_instruction));

            case VMOpCode.Cmp: return(Cmp(_instruction));

            case VMOpCode.Dup: return(Dup(_instruction));

            case VMOpCode.Int32: return(Ldc_I4(_instruction));

            case VMOpCode.Jf: return(Brfalse(_instruction));

            case VMOpCode.Jmp: return(Br(_instruction));

            case VMOpCode.Jt: return(Brtrue(_instruction));

            case VMOpCode.Ldarg: return(Ldarg(_instruction));

            case VMOpCode.Ldfld: return(Ldfld(_instruction));

            case VMOpCode.Ldloc: return(Ldloc(_instruction));

            case VMOpCode.Int64: return(Ldc_I8(_instruction));

            case VMOpCode.Newarr: return(NewArray(_instruction));

            case VMOpCode.Null: return(Null(_instruction));

            case VMOpCode.Pop: return(Pop(_instruction));

            case VMOpCode.Ret: return(Ret(_instruction));

            case VMOpCode.Stfld: return(Stfld(_instruction));

            case VMOpCode.Stloc: return(Stloc(_instruction));

            case VMOpCode.String: return(Ldstr(_instruction));


            default:
                throw new Exception($"OpCode : {vMOpCode} Not Supported");
            }
        }
Exemplo n.º 22
0
        public override VMInstruction[] GetInstructions()
        {
            VMInstruction[] objectInstructions = Object.GetInstructions();
            VMInstruction[] indexInstructions  = Index.GetInstructions();

            VMInstruction[] toReturn = new VMInstruction[objectInstructions.Length + indexInstructions.Length + 1];

            Array.Copy(objectInstructions, toReturn, objectInstructions.Length);
            Array.Copy(indexInstructions, 0, toReturn, objectInstructions.Length, indexInstructions.Length);

            toReturn[toReturn.Length - 1] = new VMInstruction(InstructionType.GetIndex);

            return(toReturn);
        }
Exemplo n.º 23
0
        private void HandleResult(VMStackFrame frame, VMInstruction instruction, VMPrimitiveExitCode result)
        {
            switch (result)
            {
            // Don't advance the instruction pointer, this primitive isnt finished yet
            case VMPrimitiveExitCode.CONTINUE_NEXT_TICK:
                ContinueExecution = false;
                break;

            case VMPrimitiveExitCode.ERROR:
                Pop(result);
                break;

            case VMPrimitiveExitCode.RETURN_TRUE:
            case VMPrimitiveExitCode.RETURN_FALSE:
                /** pop stack and return false **/
                Pop(result);
                break;

            case VMPrimitiveExitCode.GOTO_TRUE:
                MoveToInstruction(frame, instruction.TruePointer, true);
                break;

            case VMPrimitiveExitCode.GOTO_FALSE:
                MoveToInstruction(frame, instruction.FalsePointer, true);
                break;

            case VMPrimitiveExitCode.GOTO_TRUE_NEXT_TICK:
                MoveToInstruction(frame, instruction.TruePointer, false);
                break;

            case VMPrimitiveExitCode.GOTO_FALSE_NEXT_TICK:
                MoveToInstruction(frame, instruction.FalsePointer, false);
                break;

            case VMPrimitiveExitCode.CONTINUE:
                ContinueExecution = true;
                break;

            case VMPrimitiveExitCode.INTERRUPT:
                Stack.Clear();
                QueueDirty = true;
                if (Queue.Count > 0)
                {
                    Queue.RemoveAt(0);
                }
                LastStackExitCode = result;
                break;
            }
        }
Exemplo n.º 24
0
        protected void PopulateRoutineFields(BHAV bhav, VMRoutine routine)
        {
            routine.Locals    = bhav.Locals;
            routine.Arguments = bhav.Args;
            routine.Type      = bhav.Type;
            routine.ID        = bhav.ChunkID;
            routine.Chunk     = bhav;
            routine.Rti       = new VMFunctionRTI
            {
                Name = bhav.ChunkLabel
            };

            VMInstruction[] instructions = new VMInstruction[bhav.Instructions.Length];
            for (var i = 0; i < bhav.Instructions.Length; i++)
            {
                var bhavInstruction = bhav.Instructions[i];
                var instruction     = new VMInstruction();

                instruction.Index        = (byte)i;
                instruction.Opcode       = bhavInstruction.Opcode;
                instruction.Operand      = null;
                instruction.FalsePointer = bhavInstruction.FalsePointer;
                instruction.TruePointer  = bhavInstruction.TruePointer;
                instruction.Breakpoint   = bhavInstruction.Breakpoint;
                instruction.Function     = routine;

                /** Routine call **/
                if (instruction.Opcode >= 256)
                {
                    var operand = new VMSubRoutineOperand();
                    operand.Read(bhavInstruction.Operand);
                    instruction.Operand = operand;
                }
                else
                {
                    var primitive = VMContext.Primitives[instruction.Opcode];
                    if (primitive != null)
                    {
                        if (primitive.OperandModel != null)
                        {
                            VMPrimitiveOperand operand = (VMPrimitiveOperand)Activator.CreateInstance(primitive.OperandModel);
                            operand.Read(bhavInstruction.Operand);
                            instruction.Operand = operand;
                        }
                    }
                }
                instructions[i] = instruction;
            }
            routine.Instructions = instructions;
        }
Exemplo n.º 25
0
        override public VMInstruction[] GetInstructions()
        {
            VMInstruction[] leftInstructions  = left.GetInstructions();
            VMInstruction[] rightInstructions = right.GetInstructions();

            VMInstruction[] toReturn = new VMInstruction[leftInstructions.Length + rightInstructions.Length + 1];

            Array.Copy(leftInstructions, 0, toReturn, 0, leftInstructions.Length);
            Array.Copy(rightInstructions, 0, toReturn, leftInstructions.Length, rightInstructions.Length);

            toReturn[toReturn.Length - 1] = new VMInstruction(op.getBinaryInstruction());

            return(toReturn);
        }
Exemplo n.º 26
0
        override public VMInstruction[] GetInstructions()
        {
            if (value != null)
            {
                VMInstruction[] valueInstructions = value.GetInstructions();

                VMInstruction[] toReturn = new VMInstruction[valueInstructions.Length + 1];

                Array.Copy(valueInstructions, toReturn, valueInstructions.Length);

                toReturn[toReturn.Length - 1] = new VMInstruction(InstructionType.PopGlobal, varName);

                return(toReturn);
            }

            return(new VMInstruction[] { });
        }
Exemplo n.º 27
0
        public void ReadAllInstructions(VMMethod method, int instructions)
        {
            for (var i = 0; i < instructions; i++)
            {
                var instr  = new VMInstruction(VMOpCode.Nop, null, i);
                var b      = Ctx.Parser.Reader.ReadByte();
                var opCode = Ctx.PatternMatcher.GetOpCodeValue(b);
                if (opCode == VMOpCode.Nop)
                {
                    method.MethodBody.Instructions.Add(instr);
                    continue;
                }

                instr.OpCode = opCode;
                if (b > 173)
                {
                    throw new DevirtualizationException("Disassembling Exception!");
                }

                var operand = Ctx.Parser.Operands[b];
                instr.Operand = operand switch
                {
                    1 => Ctx.Parser.ReadEncryptedByte(),
                    2 => Ctx.Parser.Reader.ReadInt64(),
                    3 => Ctx.Parser.Reader.ReadSingle(),
                    4 => Ctx.Parser.Reader.ReadDouble(),
                    5 => ((Func <object>)(() =>
                    {
                        var num9 = Ctx.Parser.ReadEncryptedByte();
                        var array = new int[num9];
                        for (var m = 0; m < num9; m++)
                        {
                            array[m] = Ctx.Parser.ReadEncryptedByte();
                        }
                        return(array);
                    }))(),
                    _ => null
                };
                method.MethodBody.Instructions.Add(instr);
            }
        }
    }
Exemplo n.º 28
0
        public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper)
        {
            var buf = new byte[8];

            buf[0] = (byte)VMOpCode.Call;
            var(referenceId, method) = (Tuple <short, MethodDef>)instruction.Operand;

            if (!body.Translated.ContainsKey(method))
            {
                Array.Copy(BitConverter.GetBytes(referenceId), 0, buf, 1, 2);
                Array.Copy(BitConverter.GetBytes(TokenGetter.GetMdToken(method)), 0, buf, 3, 4);
                buf[7] = 0;
                return(buf);
            }

            Array.Copy(BitConverter.GetBytes((short)body.MethodToIndex[method]), 0, buf, 1, 2);
            Array.Copy(BitConverter.GetBytes(method.Parameters.Count), 0, buf, 3, 4);
            buf[7] = 1;
            return(buf);
        }
Exemplo n.º 29
0
        public static Instruction Ldarg(VMInstruction instruction)
        {
            short num = (short)instruction.Operand;

            switch (num)
            {
            case 0:
                return(new Instruction(OpCodes.Ldarg_0));

            case 1:
                return(new Instruction(OpCodes.Ldarg_1));

            case 2:
                return(new Instruction(OpCodes.Ldarg_2));

            case 3:
                return(new Instruction(OpCodes.Ldarg_3));

            default:
                return(new Instruction(OpCodes.Ldarg_S, num));
            }
        }
Exemplo n.º 30
0
 private void HandleResult(VMStackFrame frame, VMInstruction instruction, VMPrimitiveExitCode result)
 {
     switch (result)
     {
         /** Dont advance the instruction pointer, this primitive isnt finished yet **/
         case VMPrimitiveExitCode.CONTINUE_NEXT_TICK:
             break;
         case VMPrimitiveExitCode.ERROR:
             Pop(result);
             break;
         case VMPrimitiveExitCode.RETURN_TRUE:
         case VMPrimitiveExitCode.RETURN_FALSE:
             /** pop stack and return false **/
             Pop(result);
             break;
         case VMPrimitiveExitCode.GOTO_TRUE:
             MoveToInstruction(frame, instruction.TruePointer, true);
             break;
         case VMPrimitiveExitCode.GOTO_FALSE:
             MoveToInstruction(frame, instruction.FalsePointer, true);
             break;
         case VMPrimitiveExitCode.GOTO_TRUE_NEXT_TICK:
             MoveToInstruction(frame, instruction.TruePointer, false);
             break;
         case VMPrimitiveExitCode.GOTO_FALSE_NEXT_TICK:
             MoveToInstruction(frame, instruction.FalsePointer, false);
             break;
     }
 }
Exemplo n.º 31
0
 public byte[] Serialize(VMBody body, VMInstruction instruction, Offsets helper) =>
 new[] { (byte)VMOpCode.Dup };
Exemplo n.º 32
0
 public VMInstructionDisplay(VMInstruction inst)
 {
     this.Primitive = inst.Function.VM.Context.GetPrimitive(inst.Opcode);
     //vm.Context.GetPrimitive(inst.Opcode)
     this.Instruction = inst;
 }
Exemplo n.º 33
0
 private void HandleResult(VMStackFrame frame, VMInstruction instruction, VMPrimitiveExitCode result)
 {
     switch (result)
     {
         // Don't advance the instruction pointer, this primitive isnt finished yet
         case VMPrimitiveExitCode.CONTINUE_NEXT_TICK:
             ContinueExecution = false;
             break;
         case VMPrimitiveExitCode.ERROR:
             Pop(result);
             break;
         case VMPrimitiveExitCode.RETURN_TRUE:
         case VMPrimitiveExitCode.RETURN_FALSE:
             /** pop stack and return false **/
             Pop(result);
             break;
         case VMPrimitiveExitCode.GOTO_TRUE:
             MoveToInstruction(frame, instruction.TruePointer, true);
             break;
         case VMPrimitiveExitCode.GOTO_FALSE:
             MoveToInstruction(frame, instruction.FalsePointer, true);
             break;
         case VMPrimitiveExitCode.GOTO_TRUE_NEXT_TICK:
             MoveToInstruction(frame, instruction.TruePointer, false);
             break;
         case VMPrimitiveExitCode.GOTO_FALSE_NEXT_TICK:
             MoveToInstruction(frame, instruction.FalsePointer, false);
             break;
         case VMPrimitiveExitCode.CONTINUE:
             ContinueExecution = true;
             break;
         case VMPrimitiveExitCode.INTERRUPT:
             Stack.Clear();
             if (Queue.Count > 0) Queue.RemoveAt(0);
             LastStackExitCode = result;
             break;
     }
 }