Example #1
0
        public VMFlags ToFlags(byte value)
        {
            VMFlags result = 0;

            foreach (var entry in Flags)
            {
                if ((value & entry.Key) != 0)
                {
                    result |= entry.Value;
                }
            }

            return(result);
        }
Example #2
0
        public byte GetFlagMask(VMFlags flags)
        {
            byte result = 0;

            foreach (var entry in Flags)
            {
                if (flags.HasFlag(entry.Value))
                {
                    result |= entry.Key;
                }
            }

            return(result);
        }
Example #3
0
 public static bool HasFlagFast(this VMFlags value, VMFlags flag) => (value & flag) != 0;
Example #4
0
        private bool ExecuteCode(List<VMPreparedCommand> preparedCode,
        int cmdCount)
        {
            maxOpCount = 25000000;
            this.codeSize = cmdCount;
            this.IP = 0;

            while (true)
            {
                VMPreparedCommand cmd = preparedCode[IP];
                int op1 = GetOperand(cmd.Op1);
                int op2 = GetOperand(cmd.Op2);
                switch (cmd.OpCode)
                {

                    case VMCommands.VM_MOV:
                        SetValue(cmd.IsByteMode, Mem, op1, GetValue(cmd.IsByteMode, Mem, op2)); // SET_VALUE(Cmd->ByteMode,Op1,GET_VALUE(Cmd->ByteMode,Op2));
                        break;

                    case VMCommands.VM_MOVB:
                        SetValue(true, Mem, op1, GetValue(true, Mem, op2));
                        break;

                    case VMCommands.VM_MOVD:
                        SetValue(false, Mem, op1, GetValue(false, Mem, op2));
                        break;


                    case VMCommands.VM_CMP:
                        {
                            VMFlags value1 = (VMFlags)GetValue(cmd.IsByteMode, Mem, op1);
                            VMFlags result = value1 - GetValue(cmd.IsByteMode, Mem, op2);

                            if (result == 0)
                            {
                                flags = VMFlags.VM_FZ;
                            }
                            else
                            {
                                flags = (VMFlags)((result > value1) ? 1 : 0 | (int)(result & VMFlags.VM_FS));
                            }
                        }
                        break;


                    case VMCommands.VM_CMPB:
                        {
                            VMFlags value1 = (VMFlags)GetValue(true, Mem, op1);
                            VMFlags result = value1 - GetValue(true, Mem, op2);
                            if (result == 0)
                            {
                                flags = VMFlags.VM_FZ;
                            }
                            else
                            {
                                flags = (VMFlags)((result > value1) ? 1 : 0 | (int)(result & VMFlags.VM_FS));
                            }
                        }
                        break;

                    case VMCommands.VM_CMPD:
                        {
                            VMFlags value1 = (VMFlags)GetValue(false, Mem, op1);
                            VMFlags result = value1 - GetValue(false, Mem, op2);
                            if (result == 0)
                            {
                                flags = VMFlags.VM_FZ;
                            }
                            else
                            {
                                flags = (VMFlags)((result > value1) ? 1 : 0 | (int)(result & VMFlags.VM_FS));
                            }
                        }
                        break;


                    case VMCommands.VM_ADD:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int result = (int)((((long)value1 + (long)GetValue(cmd.IsByteMode, Mem, op2))) & unchecked((int)0xffffffff));
                            if (cmd.IsByteMode)
                            {
                                result &= 0xff;
                                flags = (VMFlags)((result < value1) ? 1 : 0 | (result == 0 ? (int)VMFlags.VM_FZ : (((result & 0x80) != 0) ? (int)VMFlags.VM_FS : 0)));
                                // Flags=(Result<Value1)|(Result==0 ? VM_FZ:((Result&0x80) ?
                                // VM_FS:0));
                            }
                            else
                                flags = (VMFlags)((result < value1) ? 1 : 0 | (result == 0 ? (int)VMFlags.VM_FZ : (result & (int)VMFlags.VM_FS)));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;


                    case VMCommands.VM_ADDB:
                        SetValue(true, Mem, op1, (int)((long)GetValue(true, Mem, op1) & 0xFFffFFff + (long)GetValue(true, Mem, op2) & unchecked((int)0xFFffFFff)));
                        break;

                    case VMCommands.VM_ADDD:
                        SetValue(false, Mem, op1, (int)((long)GetValue(false, Mem, op1) & 0xFFffFFff + (long)GetValue(false, Mem, op2) & unchecked((int)0xFFffFFff)));
                        break;


                    case VMCommands.VM_SUB:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int result = (int)((long)value1 & 0xffFFffFF - (long)GetValue(cmd.IsByteMode, Mem, op2) & unchecked((int)0xFFffFFff));
                            flags = (VMFlags)((result == 0) ? (int)VMFlags.VM_FZ : ((result > value1) ? 1 : 0 | (result & (int)VMFlags.VM_FS)));
                            SetValue(cmd.IsByteMode, Mem, op1, result); // (Cmd->ByteMode,Op1,Result);
                        }
                        break;


                    case VMCommands.VM_SUBB:
                        SetValue(true, Mem, op1, (int)((long)GetValue(true, Mem, op1) & 0xFFffFFff - (long)GetValue(true, Mem, op2) & unchecked((int)0xFFffFFff)));
                        break;

                    case VMCommands.VM_SUBD:
                        SetValue(false, Mem, op1, (int)((long)GetValue(false, Mem, op1) & 0xFFffFFff - (long)GetValue(false, Mem, op2) & unchecked((int)0xFFffFFff)));
                        break;


                    case VMCommands.VM_JZ:
                        if ((flags & VMFlags.VM_FZ) != 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_JNZ:
                        if ((flags & VMFlags.VM_FZ) == 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_INC:
                        {
                            int result = (int)((long)GetValue(cmd.IsByteMode, Mem, op1) & 0xFFffFFffL + 1L);
                            if (cmd.IsByteMode)
                            {
                                result &= 0xff;
                            }

                            SetValue(cmd.IsByteMode, Mem, op1, result);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : result & (int)VMFlags.VM_FS);
                        }
                        break;


                    case VMCommands.VM_INCB:
                        SetValue(true, Mem, op1, (int)((long)GetValue(true, Mem, op1) & 0xFFffFFffL + 1L));
                        break;

                    case VMCommands.VM_INCD:
                        SetValue(false, Mem, op1, (int)((long)GetValue(false, Mem, op1) & 0xFFffFFffL + 1L));
                        break;


                    case VMCommands.VM_DEC:
                        {
                            int result = (int)((long)GetValue(cmd.IsByteMode, Mem, op1) & 0xFFffFFff - 1);
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : result & (int)VMFlags.VM_FS);
                        }
                        break;


                    case VMCommands.VM_DECB:
                        SetValue(true, Mem, op1, (int)((long)GetValue(true, Mem, op1) & 0xFFffFFff - 1));
                        break;

                    case VMCommands.VM_DECD:
                        SetValue(false, Mem, op1, (int)((long)GetValue(false, Mem, op1) & 0xFFffFFff - 1));
                        break;


                    case VMCommands.VM_JMP:
                        setIP(GetValue(false, Mem, op1));
                        continue;

                    case VMCommands.VM_XOR:
                        {
                            int result = GetValue(cmd.IsByteMode, Mem, op1) ^ GetValue(cmd.IsByteMode, Mem, op2);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : result & (int)VMFlags.VM_FS);
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_AND:
                        {
                            int result = GetValue(cmd.IsByteMode, Mem, op1) & GetValue(cmd.IsByteMode, Mem, op2);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : result & (int)VMFlags.VM_FS);
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_OR:
                        {
                            int result = GetValue(cmd.IsByteMode, Mem, op1) | GetValue(cmd.IsByteMode, Mem, op2);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : result & (int)VMFlags.VM_FS);
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_TEST:
                        {
                            int result = GetValue(cmd.IsByteMode, Mem, op1) & GetValue(cmd.IsByteMode, Mem, op2);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : result & (int)VMFlags.VM_FS);
                        }
                        break;

                    case VMCommands.VM_JS:
                        if ((flags & VMFlags.VM_FS) != 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_JNS:
                        if ((flags & VMFlags.VM_FS) == 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_JB:
                        if ((flags & VMFlags.VM_FC) != 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_JBE:
                        if ((flags & (VMFlags.VM_FC | VMFlags.VM_FZ)) != 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_JA:
                        if ((flags & (VMFlags.VM_FC | VMFlags.VM_FZ)) == 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_JAE:
                        if ((flags & VMFlags.VM_FC) == 0)
                        {
                            setIP(GetValue(false, Mem, op1));
                            continue;
                        }
                        break;

                    case VMCommands.VM_PUSH:
                        R[7] -= 4;
                        SetValue(false, Mem, R[7] & VM_MEMMASK, GetValue(false, Mem, op1));
                        break;

                    case VMCommands.VM_POP:
                        SetValue(false, Mem, op1, GetValue(false, Mem, R[7] & VM_MEMMASK));
                        R[7] += 4;
                        break;

                    case VMCommands.VM_CALL:
                        R[7] -= 4;
                        SetValue(false, Mem, R[7] & VM_MEMMASK, IP + 1);
                        setIP(GetValue(false, Mem, op1));
                        continue;

                    case VMCommands.VM_NOT:
                        SetValue(cmd.IsByteMode, Mem, op1, ~GetValue(cmd.IsByteMode, Mem, op1));
                        break;

                    case VMCommands.VM_SHL:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int value2 = GetValue(cmd.IsByteMode, Mem, op2);
                            int result = value1 << value2;
                            flags = (VMFlags)((result == 0 ? (int)VMFlags.VM_FZ : (result & (int)VMFlags.VM_FS)) | (((value1 << (value2 - 1)) & unchecked((int)0x80000000)) != 0 ? (int)VMFlags.VM_FC : 0));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_SHR:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int value2 = GetValue(cmd.IsByteMode, Mem, op2);
                            int result = Utilities.URShift(value1, value2);
                            flags = (VMFlags)((result == 0 ? (int)VMFlags.VM_FZ : (result & (int)VMFlags.VM_FS)) | ((Utilities.URShift(value1, (value2 - 1))) & (int)VMFlags.VM_FC));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_SAR:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int value2 = GetValue(cmd.IsByteMode, Mem, op2);
                            int result = ((int)value1) >> value2;
                            flags = (VMFlags)((result == 0 ? (int)VMFlags.VM_FZ : (result & (int)VMFlags.VM_FS)) | ((value1 >> (value2 - 1)) & (int)VMFlags.VM_FC));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_NEG:
                        {
                            int result = -GetValue(cmd.IsByteMode, Mem, op1);
                            flags = (VMFlags)(result == 0 ? (int)VMFlags.VM_FZ : (int)VMFlags.VM_FC | (result & (int)VMFlags.VM_FS));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;


                    case VMCommands.VM_NEGB:
                        SetValue(true, Mem, op1, -GetValue(true, Mem, op1));
                        break;

                    case VMCommands.VM_NEGD:
                        SetValue(false, Mem, op1, -GetValue(false, Mem, op1));
                        break;

                    case VMCommands.VM_PUSHA:
                        {
                            for (int i = 0, SP = R[7] - 4; i < regCount; i++, SP -= 4)
                            {
                                SetValue(false, Mem, SP & VM_MEMMASK, R[i]);
                            }
                            R[7] -= regCount * 4;
                        }
                        break;

                    case VMCommands.VM_POPA:
                        {
                            for (int i = 0, SP = R[7]; i < regCount; i++, SP += 4)
                                R[7 - i] = GetValue(false, Mem, SP & VM_MEMMASK);
                        }
                        break;

                    case VMCommands.VM_PUSHF:
                        R[7] -= 4;
                        SetValue(false, Mem, R[7] & VM_MEMMASK, (int)flags);
                        break;

                    case VMCommands.VM_POPF:
                        flags = (VMFlags)GetValue(false, Mem, R[7] & VM_MEMMASK);
                        R[7] += 4;
                        break;

                    case VMCommands.VM_MOVZX:
                        SetValue(false, Mem, op1, GetValue(true, Mem, op2));
                        break;

                    case VMCommands.VM_MOVSX:
                        SetValue(false, Mem, op1, (byte)GetValue(true, Mem, op2));
                        break;

                    case VMCommands.VM_XCHG:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            SetValue(cmd.IsByteMode, Mem, op1, GetValue(cmd.IsByteMode, Mem, op2));
                            SetValue(cmd.IsByteMode, Mem, op2, value1);
                        }
                        break;

                    case VMCommands.VM_MUL:
                        {
                            int result = (int)(((long)GetValue(cmd.IsByteMode, Mem, op1) & 0xFFffFFff * (long)GetValue(cmd.IsByteMode, Mem, op2) & unchecked((int)0xFFffFFff)) & unchecked((int)0xFFffFFff));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_DIV:
                        {
                            int divider = GetValue(cmd.IsByteMode, Mem, op2);
                            if (divider != 0)
                            {
                                int result = GetValue(cmd.IsByteMode, Mem, op1) / divider;
                                SetValue(cmd.IsByteMode, Mem, op1, result);
                            }
                        }
                        break;

                    case VMCommands.VM_ADC:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int FC = (int)(flags & VMFlags.VM_FC);
                            int result = (int)((long)value1 & 0xFFffFFff + (long)GetValue(cmd.IsByteMode, Mem, op2) & 0xFFffFFff + (long)FC & unchecked((int)0xFFffFFff));
                            if (cmd.IsByteMode)
                            {
                                result &= 0xff;
                            }

                            flags = (VMFlags)((result < value1 || result == value1 && FC != 0) ? 1 : 0 | (result == 0 ? (int)VMFlags.VM_FZ : (result & (int)VMFlags.VM_FS)));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;

                    case VMCommands.VM_SBB:
                        {
                            int value1 = GetValue(cmd.IsByteMode, Mem, op1);
                            int FC = (int)(flags & VMFlags.VM_FC);
                            int result = (int)((long)value1 & 0xFFffFFff - (long)GetValue(cmd.IsByteMode, Mem, op2) & 0xFFffFFff - (long)FC & unchecked((int)0xFFffFFff));
                            if (cmd.IsByteMode)
                            {
                                result &= 0xff;
                            }
                            flags = (VMFlags)((result > value1 || result == value1 && FC != 0) ? 1 : 0 | (result == 0 ? (int)VMFlags.VM_FZ : (result & (int)VMFlags.VM_FS)));
                            SetValue(cmd.IsByteMode, Mem, op1, result);
                        }
                        break;


                    case VMCommands.VM_RET:
                        if (R[7] >= VM_MEMSIZE)
                        {
                            return (true);
                        }
                        setIP(GetValue(false, Mem, R[7] & VM_MEMMASK));
                        R[7] += 4;
                        continue;


                    case VMCommands.VM_STANDARD:
                        ExecuteStandardFilter((VMStandardFilters)(cmd.Op1.Data));
                        break;

                    case VMCommands.VM_PRINT:
                        break;
                }
                IP++;
                --maxOpCount;
            }
        }
Example #5
0
        public void execute(VMPreparedProgram prg)
        {
            for (int i = 0; i < prg.InitR.Length; i++)
            // memcpy(R,Prg->InitR,sizeof(Prg->InitR));
            {
                R[i] = prg.InitR[i];
            }

            long globalSize = Math.Min(prg.GlobalData.Count, VM_GLOBALMEMSIZE) & unchecked((int)0xffFFffFF);
            if (globalSize != 0)
            {
                for (int i = 0; i < globalSize; i++)
                // memcpy(Mem+VM_GLOBALMEMADDR,&Prg->GlobalData[0],GlobalSize);
                {
                    Mem[VM_GLOBALMEMADDR + i] = prg.GlobalData[i];
                }
            }
            long staticSize = Math.Min(prg.StaticData.Count, VM_GLOBALMEMSIZE - globalSize) & unchecked((int)0xffFFffFF);
            if (staticSize != 0)
            {
                for (int i = 0; i < staticSize; i++)
                // memcpy(Mem+VM_GLOBALMEMADDR+GlobalSize,&Prg->StaticData[0],StaticSize);
                {
                    Mem[VM_GLOBALMEMADDR + (int)globalSize + i] = prg.StaticData[i];
                }
            }
            R[7] = VM_MEMSIZE;
            flags = 0;

            //UPGRADE_NOTE: There is an untranslated Statement.  Please refer to original code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1153'"
            List<VMPreparedCommand> preparedCode = prg.AltCommands.Count != 0 ? prg
                .AltCommands
                : prg.Commands;

            if (!ExecuteCode(preparedCode, prg.CommandCount))
            {
                preparedCode[0].OpCode = VMCommands.VM_RET;
            }
            int newBlockPos = GetValue(false, Mem, VM_GLOBALMEMADDR + 0x20) & VM_MEMMASK;
            int newBlockSize = GetValue(false, Mem, VM_GLOBALMEMADDR + 0x1c) & VM_MEMMASK;
            if ((newBlockPos + newBlockSize) >= VM_MEMSIZE)
            {
                newBlockPos = 0;
                newBlockSize = 0;
            }

            prg.FilteredDataOffset = newBlockPos;
            prg.FilteredDataSize = newBlockSize;

            prg.GlobalData.Clear();

            int dataSize = System.Math.Min(GetValue(false, Mem, VM_GLOBALMEMADDR + 0x30), VM_GLOBALMEMSIZE - VM_FIXEDGLOBALSIZE);
            if (dataSize != 0)
            {
                //prg.GlobalData.Clear();
                // ->GlobalData.Add(dataSize+VM_FIXEDGLOBALSIZE);

                for (int i = 0; i < dataSize + VM_FIXEDGLOBALSIZE; i++)
                // memcpy(&Prg->GlobalData[0],&Mem[VM_GLOBALMEMADDR],DataSize+VM_FIXEDGLOBALSIZE);
                {
                    prg.GlobalData[i] = Mem[VM_GLOBALMEMADDR + i];
                }
            }
        }
Example #6
0
 public static bool HasFlagFast(this VMFlags value, VMFlags flag)
 {
     return((value & flag) == flag);
 }
        private bool ValidateRemainingRelationalNodes(MatchResult match, ILVariableExpression variable, VMFlags flags,
                                                      out ILAssignmentStatement varAssignment, out ILExpression flAssignment2, out MatchResult cmpMatch)
        {
            varAssignment = null;
            flAssignment2 = null;
            cmpMatch      = null;

            // Check if __AND_DWORD(var, var)
            var(left, right) = GetOperands(match);
            if (left.Variable != right.Variable ||
                left.Variable != variable.Variable ||
                left.Variable.AssignedBy.Count != 1)
            {
                return(false);
            }

            // Check if __AND_DWORD(fl2, overflow | sign | zero)
            varAssignment = left.Variable.AssignedBy[0];
            var flMatch = FLAndConstantPattern.Match(varAssignment.Value);

            if (!flMatch.Success)
            {
                return(false);
            }

            var constant3 = (ILInstructionExpression)flMatch.Captures["constant"][0];

            if ((byte)(uint)constant3.Operand != _constants.GetFlagMask(flags))
            {
                return(false);
            }

            // Check if CMP_xxxx(op0, op1)
            var flUsage2 = (ILFlagsVariable)((ILVariableExpression)flMatch.Captures["fl"][0]).Variable;

            if (flUsage2.ImplicitAssignments.Count != 1)
            {
                return(false);
            }

            flAssignment2 = flUsage2.ImplicitAssignments.First();
            cmpMatch      = ComparePattern.Match(flAssignment2);
            if (!cmpMatch.Success)
            {
                return(false);
            }

            return(true);
        }
        private bool TryOptimizeFlagComparison(ILCompilationUnit unit, ILVariableExpression flagUsage, ILFlagsVariable fl, VMFlags flags)
        {
            // TODO: support other flag combinations.
            bool changed = false;

            switch (flags)
            {
            case VMFlags.ZERO:
                changed |= TryOptimizeCompareWithZero(unit, fl, flagUsage);
                break;
            }

            return(changed);
        }
Example #9
0
 public int this[VMFlags flag] {
     get { return(flagOrder[(int)flag]); }
 }
Example #10
0
 public int this[VMFlags flag] => flagOrder[(int)flag];