Пример #1
0
        X86Instruction[] targetProgram; // program which has to be optimized

        static void interpret(X86ExecutionContext ctx, X86Instruction[] program)
        {
            for (int instrI = 0; instrI < program.Length; instrI++)
            {
                X86Instruction instr = program[instrI];
                ctx.interpret(instr);
            }
        }
Пример #2
0
        public void enumerateStep()
        {
            // decode
            bool programIsValid;

            X86Instruction[] trailProgram = decodeProgram(instructionEnumeration, out programIsValid);

            if (programIsValid)
            {
                // run the target program and the trailProgram and compare results

                IList <X86ExecutionContext> executionContextsToCheck = new List <X86ExecutionContext>();
                executionContextsToCheck.Add(new X86ExecutionContext(8, 8));

                /*
                 * executionContextsToCheck.Add(new ExecutionContext(8, 8));
                 * executionContextsToCheck[0].integerRegisters[0] = 3;
                 * executionContextsToCheck[0].integerRegisters[1] = 5;
                 * executionContextsToCheck[1].integerRegisters[0] = 4;
                 * executionContextsToCheck[1].integerRegisters[1] = 5;
                 */
                executionContextsToCheck[0].vectorRegisters[0] = new float[4] {
                    1.0f, 0.1f, 0.9f, 3.76f
                };

                bool equalResultForAllContext = true;

                foreach (var executionCtxI in executionContextsToCheck)
                {
                    X86ExecutionContext execCtxTarget = executionCtxI.deepClone();
                    interpret(execCtxTarget, targetProgram);

                    X86ExecutionContext execCtxTrail = executionCtxI.deepClone();
                    interpret(execCtxTrail, trailProgram);

                    /*if( execCtxTarget.integerRegisters[0] != execCtxTrail.integerRegisters[0] || execCtxTarget.integerRegisters[1] != execCtxTrail.integerRegisters[1] ) {
                     *  equalResultForAllContext = false;
                     *  break;
                     * }*/

                    if (execCtxTarget.vectorRegisters[0][0] != execCtxTrail.vectorRegisters[0][0] ||
                        execCtxTarget.vectorRegisters[0][1] != execCtxTrail.vectorRegisters[0][1] ||
                        execCtxTarget.vectorRegisters[0][2] != execCtxTrail.vectorRegisters[0][2] ||
                        execCtxTarget.vectorRegisters[0][3] != execCtxTrail.vectorRegisters[0][3]
                        )
                    {
                        equalResultForAllContext = false;
                        break;
                    }
                }

                if (equalResultForAllContext)
                {
                    int here = 5;
                    throw new Exception("FOUND");
                }
            }

            // increment and carry increment

            if (false)
            {
                Console.WriteLine("enumBefore  {0} {1} {2} {3}     {4} {5} {6} {7}  ", instructionEnumeration[0], instructionEnumeration[1], instructionEnumeration[2], instructionEnumeration[3], instructionEnumeration[4], instructionEnumeration[5], instructionEnumeration[6], instructionEnumeration[7]);
            }


            for (int instructionIdx = 0; instructionIdx < 3; instructionIdx++)
            {
                int enumerationIdx = instructionIdx * widthOfInstructionEncoding;

                int idxOfImmediate2Encoding = enumerationIdx + 0;
                int idxOfTypeEncoding       = enumerationIdx + 1;
                int idxOfDestEncoding       = enumerationIdx + 2;
                int idxOfAEncoding          = enumerationIdx + 3;

                var typeOfInstruction = decodeInstructionType(instructionEnumeration[idxOfTypeEncoding]);

                if (X86Instruction.doesInstructionNeedImmediate2(typeOfInstruction))
                {
                    instructionEnumeration[idxOfImmediate2Encoding]++;

                    // check if we don't need to carry
                    if (instructionEnumeration[idxOfImmediate2Encoding] < maximalValue[idxOfImmediate2Encoding])
                    {
                        break;
                    }

                    // we need to carry
                    instructionEnumeration[idxOfImmediate2Encoding] = 0;
                    instructionEnumeration[idxOfTypeEncoding]++;
                }
                else
                {
                    instructionEnumeration[idxOfTypeEncoding]++;
                }

                // check if we don't need to carry for the type
                if (instructionEnumeration[idxOfTypeEncoding] < maximalValue[idxOfTypeEncoding])
                {
                    break;
                }


                // we need to carry
                instructionEnumeration[idxOfTypeEncoding] = 0;
                instructionEnumeration[idxOfDestEncoding]++;

                if (instructionEnumeration[idxOfDestEncoding] < maximalValue[idxOfDestEncoding])
                {
                    break;
                }

                // we need to carry
                instructionEnumeration[idxOfDestEncoding] = 0;
                instructionEnumeration[idxOfAEncoding]++;



                if (instructionEnumeration[idxOfAEncoding] < maximalValue[idxOfAEncoding])
                {
                    break;
                }

                // we need to carry
                instructionEnumeration[idxOfAEncoding] = 0;

                // if we are here we carry into the next instruction
            }

            if (false)
            {
                Console.WriteLine("enumAfter  {0} {1} {2} {3}     {4} {5} {6} {7}  ", instructionEnumeration[0], instructionEnumeration[1], instructionEnumeration[2], instructionEnumeration[3], instructionEnumeration[4], instructionEnumeration[5], instructionEnumeration[6], instructionEnumeration[7]);
            }


            // check for wrap around
            bool allZero = true;

            for (int enumerationIdx = 0; enumerationIdx < instructionEnumeration.Length; enumerationIdx++)
            {
                if (instructionEnumeration[enumerationIdx] != 0)
                {
                    allZero = false;
                    break;
                }
            }

            if (allZero)    // if this is true then we are finished with the enumeration of the program with the length
            {
                int here5605 = 5;
                throw new Exception("WRAP AROUND");
            }
        }
Пример #3
0
        public void call(PatternInterpretationContext interpretationCtx, IList <PatternWithDecoration> arguments)
        {
            PatternWithDecoration resultPattern = arguments[arguments.Count - 1];

            PatternInterpreter.vmAssert(
                resultPattern.type == PatternWithDecoration.EnumType.VARIABLE,
                false,
                "Last argument must be result which is a variable");

            X86ExecutionContext ctx = new X86ExecutionContext(8, 8);

            // translate arguments to float values in context
            // the first value of the vector is used
            for (int argI = 0; argI < arguments.Count - 1; argI++)
            {
                PatternWithDecoration argument = arguments[argI];
                PatternWithDecoration argumentValue;
                if (argument.type == PatternWithDecoration.EnumType.VARIABLE)
                {
                    PatternInterpreter.vmAssert(
                        interpretationCtx.valueByVariable[argument.variableId].decoration.type == pattern.Decoration.EnumType.VALUE,
                        false,
                        "Variable must be value");

                    argumentValue = interpretationCtx.valueByVariable[argument.variableId];
                }
                else
                {
                    argumentValue = argument;
                }

                PatternInterpreter.vmAssert(
                    argumentValue.decoration.type == pattern.Decoration.EnumType.VALUE,
                    false,
                    "Argument must be value");

                PatternInterpreter.vmAssert(
                    argumentValue.decoration.value is float,
                    false,
                    "Argument must be float value");

                ctx.vectorRegisters[argI][0] = (float)argumentValue.decoration.value;
            }

            // interpret program
            for (int i = 0; i < instructions.Length; i++)
            {
                ctx.interpret(instructions[i]);
            }


            float floatResult = ctx.vectorRegisters[0][0];

            // translate result
            if (interpretationCtx.valueByVariable.ContainsKey(resultPattern.variableId))
            {
                interpretationCtx.valueByVariable[resultPattern.variableId].decoration.value = floatResult;
            }
            else
            {
                interpretationCtx.valueByVariable[resultPattern.variableId]                  = new PatternWithDecoration();
                interpretationCtx.valueByVariable[resultPattern.variableId].decoration       = new pattern.Decoration();
                interpretationCtx.valueByVariable[resultPattern.variableId].decoration.value = floatResult;
            }
        }