Ejemplo n.º 1
0
        public static void movImmediate(LocalInterpreterState state, int register, int value, out bool success)
        {
            state.registers[register] = value;

            state.instructionPointer++;
            success = true;
        }
Ejemplo n.º 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++;
 }
Ejemplo n.º 3
0
 public static void jumpIfFlag(LocalInterpreterState state, int delta)
 {
     if (state.comparisionFlag)
     {
         state.instructionPointer += delta;
     }
     state.instructionPointer++;
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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++;
        }
Ejemplo n.º 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++;
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 16
0
 public static void add(LocalInterpreterState state, int register, int value)
 {
     state.registers[register] += value;
     state.instructionPointer++;
 }
Ejemplo n.º 17
0
 public static void arrayRemove(GlobalInterpreterState globalState, LocalInterpreterState localState, int dummy0, int dummy1, out bool success)
 {
     ArrayOperations.arrayRemove(globalState, localState, out success);
 }
Ejemplo n.º 18
0
 public static void mulRegisterImmediate(LocalInterpreterState state, int register, int value)
 {
     state.registers[register] *= value;
     state.instructionPointer++;
 }
Ejemplo n.º 19
0
 public static void call(LocalInterpreterState state, int delta)
 {
     state.callstack.push(state.instructionPointer + 1);
     state.instructionPointer += delta;
     state.instructionPointer++;
 }
Ejemplo n.º 20
0
 public static void arrayCompareWithRegister(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, int dummy0, out bool success)
 {
     ArrayOperations.arrayCompareWithRegister(globalState, localState, register, out success);
 }
Ejemplo n.º 21
0
 public static void jump(LocalInterpreterState state, int delta)
 {
     state.instructionPointer += delta;
     state.instructionPointer++;
 }
Ejemplo n.º 22
0
 public static void subRegisterRegister(LocalInterpreterState state, int registerDestination, int registerSource)
 {
     state.registers[registerDestination] -= state.registers[registerSource];
     state.instructionPointer++;
 }
Ejemplo n.º 23
0
 public static void @return(LocalInterpreterState state, out bool success)
 {
     state.instructionPointer = state.callstack.pop(out success);
 }
Ejemplo n.º 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++;
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 26
0
 public static void jump(LocalInterpreterState state, int delta, int dummy0, out bool success)
 {
     Operations.jump(state, delta);
     success = true;
 }
Ejemplo n.º 27
0
 public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta)
 {
     globalState.arrayState.index += delta;
     localState.instructionPointer++;
 }
Ejemplo n.º 28
0
 public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta, int dummy, out bool success)
 {
     ArrayOperations.arrayMove(globalState, localState, delta);
     success = true;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
            }
        }