コード例 #1
0
        public static void movImmediate(LocalInterpreterState state, int register, int value, out bool success)
        {
            state.registers[register] = value;

            state.instructionPointer++;
            success = true;
        }
コード例 #2
0
 // add by checking flag
 public static void addFlag(LocalInterpreterState state, int register, int value)
 {
     if (state.comparisionFlag)
     {
         state.registers[register] += value;
     }
     state.instructionPointer++;
 }
コード例 #3
0
 public static void jumpIfFlag(LocalInterpreterState state, int delta)
 {
     if (state.comparisionFlag)
     {
         state.instructionPointer += delta;
     }
     state.instructionPointer++;
 }
コード例 #4
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;
        }
コード例 #5
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;
 }
コード例 #6
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;
        }
コード例 #7
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;
        }
コード例 #8
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;
        }
コード例 #9
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;
        }
コード例 #10
0
        // /param type 0 : equality, -1 less than, 1 greater than
        public static void compareImmediate(LocalInterpreterState state, int register, int value, int type)
        {
            if (type == 0)
            {
                state.comparisionFlag = state.registers[register] == value;
            }
            else if (type == -1)
            {
                state.comparisionFlag = state.registers[register] < value;
            }
            else
            {
                state.comparisionFlag = state.registers[register] > value;
            }

            state.instructionPointer++;
        }
コード例 #11
0
        // /param type 0 : equality, -1 less than, 1 greater than
        public static void compareRegister(LocalInterpreterState state, int registerRight, int registerLeft, int type)
        {
            if (type == 0)
            {
                state.comparisionFlag = state.registers[registerRight] == state.registers[registerLeft];
            }
            else if (type == -1)
            {
                state.comparisionFlag = state.registers[registerRight] < state.registers[registerLeft];
            }
            else
            {
                state.comparisionFlag = state.registers[registerRight] > state.registers[registerLeft];
            }

            state.instructionPointer++;
        }
コード例 #12
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);
        }
コード例 #13
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;
        }
コード例 #14
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;
        }
コード例 #15
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;
        }
コード例 #16
0
 public static void add(LocalInterpreterState state, int register, int value)
 {
     state.registers[register] += value;
     state.instructionPointer++;
 }
コード例 #17
0
 public static void arrayRemove(GlobalInterpreterState globalState, LocalInterpreterState localState, int dummy0, int dummy1, out bool success)
 {
     ArrayOperations.arrayRemove(globalState, localState, out success);
 }
コード例 #18
0
 public static void mulRegisterImmediate(LocalInterpreterState state, int register, int value)
 {
     state.registers[register] *= value;
     state.instructionPointer++;
 }
コード例 #19
0
 public static void call(LocalInterpreterState state, int delta)
 {
     state.callstack.push(state.instructionPointer + 1);
     state.instructionPointer += delta;
     state.instructionPointer++;
 }
コード例 #20
0
 public static void arrayCompareWithRegister(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, int dummy0, out bool success)
 {
     ArrayOperations.arrayCompareWithRegister(globalState, localState, register, out success);
 }
コード例 #21
0
 public static void jump(LocalInterpreterState state, int delta)
 {
     state.instructionPointer += delta;
     state.instructionPointer++;
 }
コード例 #22
0
 public static void subRegisterRegister(LocalInterpreterState state, int registerDestination, int registerSource)
 {
     state.registers[registerDestination] -= state.registers[registerSource];
     state.instructionPointer++;
 }
コード例 #23
0
 public static void @return(LocalInterpreterState state, out bool success)
 {
     state.instructionPointer = state.callstack.pop(out success);
 }
コード例 #24
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++;
 }
コード例 #25
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;
        }
コード例 #26
0
 public static void jump(LocalInterpreterState state, int delta, int dummy0, out bool success)
 {
     Operations.jump(state, delta);
     success = true;
 }
コード例 #27
0
 public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta)
 {
     globalState.arrayState.index += delta;
     localState.instructionPointer++;
 }
コード例 #28
0
 public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta, int dummy, out bool success)
 {
     ArrayOperations.arrayMove(globalState, localState, delta);
     success = true;
 }
コード例 #29
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;
 }
コード例 #30
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;
            }
        }