コード例 #1
0
        // random number up to the value of the register
        public static void random(GlobalInterpreterState globalState, LocalInterpreterState localState, int destRegister, int register, out bool success)
        {
            if (localState.registers[register] <= 0)
            {
                success = false;
                return;
            }

            localState.registers[destRegister] = globalState.rng.Next(localState.registers[register]);
            success = true;
        }
コード例 #2
0
 public static void arrayCompareWithRegister(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, out bool success)
 {
     if (globalState.arrayState == null || !globalState.arrayState.isIndexValid)
     {
         success = false;
         return;
     }
     localState.comparisionFlag = localState.registers[register] == globalState.arrayState.array[globalState.arrayState.index];
     localState.instructionPointer++;
     success = true;
 }
コード例 #3
0
 public static void arrayMovToArray(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int register, out bool success)
 {
     if (globalState.arrayState == null || !globalState.arrayState.isIndexValid)
     {
         success = false;
         return;
     }
     globalState.arrayState.array[globalState.arrayState.index] = localState.registers[register];
     localState.instructionPointer++;
     success = true;
 }
コード例 #4
0
        public static void length(GlobalInterpreterState globalState, LocalInterpreterState localState, int destRegister, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            localState.registers[destRegister] = globalState.arrayState.array.count;

            success = true;
        }
コード例 #5
0
        // /param array is the index of the array (currently ignored)
        public static void valid(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            localState.comparisionFlag = globalState.arrayState.isIndexValid;

            localState.instructionPointer++;
            success = true;
        }
コード例 #6
0
        public static void reg2idx(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, int array, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            globalState.arrayState.index = localState.registers[register];

            localState.instructionPointer++;
            success = true;
        }
コード例 #7
0
        public static void arrayRemove(GlobalInterpreterState globalState, LocalInterpreterState localState, out bool success)
        {
            if (globalState.arrayState == null || !globalState.arrayState.isIndexValid)
            {
                success = false;
                return;
            }

            globalState.arrayState.array.removeAt(globalState.arrayState.index);

            localState.instructionPointer++;

            success = true;
        }
コード例 #8
0
        // checks if the program was terminated successfully by returning to the global caller
        public static bool isTerminating(GlobalInterpreterState globalState, LocalInterpreterState localState, uint instruction)
        {
            // return
            if (localState.callstack.top == 0x0000ffff && instruction == INSTRUCTION_RET)
            {
                return(true);
            }


            if (isMacroArrayAdvanceOrExit(instruction))
            {
                bool atLastIndex = globalState.arrayState.index >= globalState.arrayState.array.count - 1;

                return(atLastIndex);
            }

            return(false);
        }
コード例 #9
0
        // array is ignored
        public static void setIdxRelative(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int index, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            if (index == -1)   // end of array, so insertion appends an element
            {
                globalState.arrayState.index = globalState.arrayState.array.count;
            }
            else
            {
                globalState.arrayState.index = index;
            }

            localState.instructionPointer++;
            success = true;
        }
コード例 #10
0
        public static void insert(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            if (globalState.arrayState.index < 0 || globalState.arrayState.index > globalState.arrayState.array.count)
            {
                success = false;
                return;
            }

            int valueToInsert = localState.registers[register];

            globalState.arrayState.array.insert(globalState.arrayState.index, valueToInsert);

            localState.instructionPointer++;
            success = true;
        }
コード例 #11
0
        // macro-arrNotEndOrExit
        // advance array index and reset and return/terminate if its over
        // else jump relative
        public static void macroArrayNotEndOrExit(GlobalInterpreterState globalState, LocalInterpreterState localState, int ipDelta, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            bool isIndexValid = globalState.arrayState.isIndexValid;

            if (!isIndexValid)
            {
                globalState.arrayState.index = 0;
                Operations.@return(localState, out success);
                return;
            }

            localState.instructionPointer++;
            localState.instructionPointer += ipDelta;

            success = true;
        }
コード例 #12
0
        // moves the array index by delta and stores in the flag if the index is still in bound after moving
        public static void idxFlag(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int delta, out bool success)
        {
            if (globalState.arrayState == null)
            {
                success = false;
                return;
            }

            if (array == 0)
            {
                globalState.arrayState.index += delta;
                localState.comparisionFlag    = globalState.arrayState.isIndexValid; // store the validity of the arrayIndex in flag
            }
            else
            {
                success = false;
                return;
            }


            localState.instructionPointer++;
            success = true;
        }
コード例 #13
0
 public static void arrayRemove(GlobalInterpreterState globalState, LocalInterpreterState localState, int dummy0, int dummy1, out bool success)
 {
     ArrayOperations.arrayRemove(globalState, localState, out success);
 }
コード例 #14
0
        public static void interpret(
            GlobalInterpreterState globalState,
            LocalInterpreterState localState,
            bool debugExecution,
            out bool programExecutedSuccessful,
            out bool hardExecutionError
            )
        {
            programExecutedSuccessful = false;
            hardExecutionError        = false;

            /*
             * if( arguments.lengthOfProgram >= 4 ) {
             *  if( arguments.program[0] == 3 && arguments.program[1] == 62 && arguments.program[2] == 2 && arguments.program[3] == 31 ) {
             *      int debugHere = 5;
             *  }
             * }
             * //*/

            if (localState.program.length >= 3)
            {
                if (localState.program[0] == 35 && localState.program[1] == 32 && localState.program[2] == InstructionInterpreter.convInstructionAndRelativeToInstruction(1, -3))
                {
                    int debugHere = 5;
                }
            }

            for (int instructionsRetired = 0; instructionsRetired < localState.maxNumberOfRetiredInstructions; instructionsRetired++)
            {
                bool instructionPointerValid = localState.instructionPointer >= 0 && localState.instructionPointer < localState.program.length;
                if (!instructionPointerValid)
                {
                    hardExecutionError = true;
                    break;
                }

                uint currentInstruction = localState.program[localState.instructionPointer];

                if (debugExecution)
                {
                    Console.WriteLine("program=");

                    throw new NotImplementedException(); // TODO< logger >
                    //Program2.debug(null, arguments.program);

                    Console.WriteLine("ip={0}", localState.instructionPointer);
                    Console.Write("arr=");

                    throw new NotImplementedException(); // TODO< logger >
                    //Program2.debug(null, arguments.interpreterState.arrayState.array);
                    Console.WriteLine("array index={0}", globalState.arrayState.index);
                }

                if (InstructionInterpreter.isTerminating(globalState, localState, currentInstruction))
                {
                    programExecutedSuccessful = true; // the program executed successfully only if we return
                    break;
                }

                bool instructionExecutedSuccessfull;
                int  indirectCallIndex;
                InstructionInterpreter.dispatch(globalState, localState, new Instruction(currentInstruction), out instructionExecutedSuccessfull, out indirectCallIndex);
                if (!instructionExecutedSuccessfull)
                {
                    hardExecutionError = true;
                    break;
                }

                if (indirectCallIndex != -1)
                {
                    // an indirect call is a call to an (interpreted) function

                    // try to dispatch indirect call
                    LocalInterpreterState indirectLocalInterpreterState;
                    if (!localState.indirectCallTable.TryGetValue((uint)indirectCallIndex, out indirectLocalInterpreterState))
                    {
                        hardExecutionError = true;
                        break;
                    }

                    indirectLocalInterpreterState.reset();

                    bool calleeExecutedSuccessfully, calleeHardExecutionError;
                    interpret(globalState, indirectLocalInterpreterState, debugExecution, out calleeExecutedSuccessfully, out calleeHardExecutionError);
                    if (calleeExecutedSuccessfully)
                    {
                        // do nothing
                    }
                    else if (calleeHardExecutionError)
                    {
                        hardExecutionError = true;
                        break;
                    }
                }
            }

            if (hardExecutionError)
            {
                programExecutedSuccessful = false;
            }
        }
コード例 #15
0
 public static void arrayCompareWithRegister(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, int dummy0, out bool success)
 {
     ArrayOperations.arrayCompareWithRegister(globalState, localState, register, out success);
 }
コード例 #16
0
 // interprets the value as binary (zero is false and all else is true) and negates it
 public static void binaryNegate(GlobalInterpreterState globalState, LocalInterpreterState localState, int register)
 {
     localState.registers[register] = (localState.registers[register] == 0) ? 1 : 0;
     localState.instructionPointer++;
 }
コード例 #17
0
 public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta, int dummy, out bool success)
 {
     ArrayOperations.arrayMove(globalState, localState, delta);
     success = true;
 }
コード例 #18
0
 public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta)
 {
     globalState.arrayState.index += delta;
     localState.instructionPointer++;
 }
コード例 #19
0
        // \param indirectCall is not -1 if the instruction is an indirect call to another function
        public static void dispatch(
            GlobalInterpreterState globalState,
            LocalInterpreterState localState,
            Instruction instr,
            out bool success,
            out int indirectCall
            )
        {
            indirectCall = -1;

            uint instructionWithoutRelative;
            int  relative;

            decodeInstruction(instr.code, out instructionWithoutRelative, out relative);

            switch (instructionWithoutRelative)
            {
            case INSTRUCTION_RET: Operations.@return(localState, out success); return;

            case 1: ArrayOperations.macroArrayAdvanceOrExit(globalState, localState, relative, out success); return;

            case 2: ArrayOperations.macroArrayNotEndOrExit(globalState, localState, relative, out success); return;

            case 3: Operations.jump(localState, relative); success = true; return;

            case 4: Operations.jumpIfNotFlag(localState, relative); success = true; return;

            case 5: Operations.call(localState, relative); success = true; return;

            case 6: ArrayOperationsTwoArgumentWrapper.arrayMove(globalState, localState, -1, int.MaxValue, out success); return;

            case 7: ArrayOperationsTwoArgumentWrapper.arrayMove(globalState, localState, 1, int.MaxValue, out success); return;

            case 8: ArrayOperationsTwoArgumentWrapper.arrayRemove(globalState, localState, int.MaxValue, int.MaxValue, out success); return;

            case 9: ArrayOperationsTwoArgumentWrapper.arrayCompareWithRegister(globalState, localState, 0, int.MaxValue, out success); return;

            case 10: ArrayOperationsTwoArgumentWrapper.arrayCompareWithRegister(globalState, localState, 1, int.MaxValue, out success); return;

            case 11: ArrayOperations.insert(globalState, localState, /*reg*/ 0, out success); return;

            case 12: ArrayOperations.insert(globalState, localState, /*reg*/ 1, out success); return;

            case 13: ArrayOperations.insert(globalState, localState, /*reg*/ 2, out success); return;

            case 14: ArrayOperations.setIdxRelative(globalState, localState, 0, 0, out success); return;

            case 15: ArrayOperations.setIdxRelative(globalState, localState, 0, -1, out success); return; // -1 is end of array

            case 16: ArrayOperations.idxFlag(globalState, localState, 0, 1, out success); return;         // TODO< should be an intrinsic command which gets added by default >

            case 17: ArrayOperations.valid(globalState, localState, /*array*/ 0, out success); return;

            case 18: ArrayOperations.read(globalState, localState, /*array*/ 0, /*register*/ 0, out success); return;

            case 19: ArrayOperations.idx2Reg(globalState, localState, /*array*/ 0, /*register*/ 0, out success); return;

            case 20: Operations.movImmediate(localState, /*register*/ 0, 0, out success); return;

            case 21: Operations.movImmediate(localState, /*register*/ 0, 1, out success); return;

            case 22: Operations.movImmediate(localState, /*register*/ 0, 3, out success); return;

            case 23: ArrayOperations.arrayMovToArray(globalState, localState, /*array*/ 0, /*register*/ 0, out success); return;

            case 24: Operations.mulRegisterImmediate(localState, /*register*/ 0, -1); success = true; return;

            case 25: Operations.binaryNegate(globalState, localState, /*register*/ 0); success = true; return;

            case 26: ArrayOperations.macroArrayAdvanceOrExit(globalState, localState, -4, out success); return;

            case 27: Operations.movImmediate(localState, /*register*/ 1, 0, out success); return;

            case 28: Operations.movImmediate(localState, /*register*/ 1, 1, out success); return;

            case 29: Operations.movImmediate(localState, /*register*/ 1, 3, out success); return;

            case 30: ArrayOperations.read(globalState, localState, /*array*/ 0, /*register*/ 1, out success); return;

            case 31: Operations.mulRegisterRegister(localState, 0, 1); success = true; return;

            case 32: Operations.addRegisterRegister(localState, 0, 1); success = true; return;

            case 33: ArrayOperations.arrayMovToArray(globalState, localState, /*array*/ 0, /*register*/ 1, out success); return;

            case 34: Operations.subRegisterRegister(localState, 1, 0); success = true; return;

            case 35: ArrayOperations.read(globalState, localState, /*array*/ 0, /*register*/ 1, out success); return;

            case 36: ArrayOperations.reg2idx(globalState, localState, /*register*/ 0, /*array*/ 0, out success); return;

            case 37: Operations.compareRegister(localState, /*register*/ 0, /*register*/ 1, /*type*/ -1); success = true; return;  // TODO< maybe using relative value as immediate >

            case 38: Operations.random(globalState, localState, 0, 0, out success); return;

            case 39: ArrayOperations.length(globalState, localState, /*destRegister*/ 0, out success); return;
            }

            // if we are here we have instrution with hardcoded parameters

            uint baseInstruction = InstructionInfo.getNumberOfHardcodedSingleInstructions();

            Debug.Assert(instructionWithoutRelative >= baseInstruction);
            int currentBaseInstruction = (int)baseInstruction;



            // add register constant
            if (instructionWithoutRelative <= currentBaseInstruction + 3)
            {
                int subInstruction = (int)instructionWithoutRelative - currentBaseInstruction; // which instruction do we choose from the pool

                if (subInstruction == 0)
                {
                    Operations.add(localState, /*register*/ 0, -1);
                    success = true;
                }
                else if (subInstruction == 1)
                {
                    Operations.add(localState, /*register*/ 0, 2);
                    success = true;
                }
                else if (subInstruction == 2)
                {
                    Operations.add(localState, /*register*/ 1, -1);
                    success = true;
                }
            }

            currentBaseInstruction += 3;


            // addFlag reg0 constant
            if (instructionWithoutRelative <= currentBaseInstruction + 1)
            {
                // currently just compare reg0 with zero
                Operations.addFlag(localState, /*register*/ 0, 1);
                success = true;
                return;
            }

            currentBaseInstruction += 1;



            // indirect table call
            if (instructionWithoutRelative <= currentBaseInstruction + 1)
            {
                // currently just compare reg0 with zero
                indirectCall = 0;
                success      = true;
                return;
            }

            currentBaseInstruction += 1;

            // additional instructions
            GlobalInterpreterState.AdditionalOperationsDelegateType additionalOperationDelegate;
            if (globalState.additionalOperations.TryGetValue(instructionWithoutRelative, out additionalOperationDelegate))
            {
                additionalOperationDelegate(globalState, localState, out success);
                return;
            }


            // unknown instruction
            success = false;
        }