Ejemplo n.º 1
0
        public virtual Expression VisitPhiFunction(PhiFunction pc)
        {
            var oldChanged = Changed;
            var args       = pc.Arguments
                             .Select(a =>
            {
                var arg = SimplifyPhiArg(a.Value.Accept(this));
                ctx.RemoveExpressionUse(arg);
                return(arg);
            })
                             .Where(a => ctx.GetValue(a as Identifier) != pc)
                             .ToArray();

            Changed = oldChanged;

            var cmp = new ExpressionValueComparer();
            var e   = args.FirstOrDefault();

            if (e != null && args.All(a => cmp.Equals(a, e)))
            {
                Changed = true;
                ctx.UseExpression(e);
                return(e);
            }
            else
            {
                ctx.UseExpression(pc);
                return(pc);
            }
        }
Ejemplo n.º 2
0
        public ExpressionSimplifier(SegmentMap segmentMap, EvaluationContext ctx, DecompilerEventListener listener)
        {
            this.segmentMap = segmentMap ?? throw new ArgumentNullException(nameof(SegmentMap));
            this.ctx        = ctx;
            this.cmp        = new ExpressionValueComparer();

            this.add2ids             = new AddTwoIdsRule(ctx);
            this.addEcc              = new Add_e_c_cRule(ctx);
            this.addMici             = new Add_mul_id_c_id_Rule(ctx);
            this.dpbConstantRule     = new DpbConstantRule();
            this.dpbdpbRule          = new DpbDpbRule(ctx);
            this.idConst             = new IdConstant(ctx, new Unifier(), listener);
            this.idCopyPropagation   = new IdCopyPropagationRule(ctx);
            this.idBinIdc            = new IdBinIdc_Rule(ctx);
            this.sliceConst          = new SliceConstant_Rule();
            this.sliceMem            = new SliceMem_Rule();
            this.sliceSegPtr         = new SliceSegmentedPointer_Rule(ctx);
            this.negSub              = new NegSub_Rule();
            this.constConstBin       = new ConstConstBin_Rule();
            this.shAdd               = new Shl_add_Rule(ctx);
            this.shMul               = new Shl_mul_e_Rule(ctx);
            this.shiftShift          = new ShiftShift_c_c_Rule(ctx);
            this.mpsRule             = new Mps_Constant_Rule(ctx);
            this.sliceShift          = new SliceShift(ctx);
            this.binopWithSelf       = new BinOpWithSelf_Rule();
            this.constDiv            = new ConstDivisionImplementedByMultiplication(ctx);
            this.selfdpbRule         = new SelfDpbRule(ctx);
            this.idProcConstRule     = new IdProcConstRule(ctx);
            this.castCastRule        = new CastCastRule(ctx);
            this.distributedCast     = new DistributedCastRule();
            this.distributedSlice    = new DistributedSliceRule();
            this.mkSeqFromSlicesRule = new MkSeqFromSlices_Rule(ctx);
            this.constOnLeft         = new ComparisonConstOnLeft();
            this.sliceSeq            = new SliceSequence(ctx);
        }
Ejemplo n.º 3
0
 public ProjectionFilter(IProcessorArchitecture arch, SsaState ssa, SegmentedAccessClassifier sac)
 {
     this.arch          = arch;
     this.ssa           = ssa;
     this.sac           = sac;
     this.NewStatements = new HashSet <Statement>();
     this.cmp           = new ExpressionValueComparer();
 }
Ejemplo n.º 4
0
 public MipsRewriter(MipsProcessorArchitecture arch, IEnumerable <MipsInstruction> instrs, IStorageBinder binder, IRewriterHost host)
 {
     this.arch   = arch;
     this.binder = binder;
     this.dasm   = instrs.GetEnumerator();
     this.host   = host;
     this.cmp    = new ExpressionValueComparer();
 }
Ejemplo n.º 5
0
 public BackwardSlicer(IBackWalkHost <RtlBlock, RtlInstruction> host)
 {
     this.host     = host;
     this.worklist = new WorkList <SliceState>();
     this.visited  = new HashSet <RtlBlock>();
     this.cmp      = new ExpressionValueComparer();
     this.simp     = new ExpressionSimplifier(host.SegmentMap, new EvalCtx(), null);
 }
Ejemplo n.º 6
0
 public SparseValuePropagation(SsaState ssa, Program program, IDynamicLinker resolver, DecompilerEventListener listener)
 {
     this.ssa      = ssa;
     this.listener = listener;
     this.cmp      = new ExpressionValueComparer();
     this.ctx      = new SparseEvaluationContext(ssa.Procedure.Architecture);
     this.eval     = new Evaluation.ExpressionSimplifier(program.SegmentMap, ctx, listener);
 }
Ejemplo n.º 7
0
 public ValueSetEvaluator(Program program, Dictionary <Expression, ValueSet> context, ProcessorState state = null)
 {
     this.program     = program;
     this.context     = context;
     this.state       = state;
     this.cmp         = new ExpressionValueComparer();
     this.memAccesses = new Dictionary <Address, DataType>();
 }
Ejemplo n.º 8
0
 public ValueSetEvaluator(IProcessorArchitecture arch, SegmentMap segmentMap, Dictionary <Expression, ValueSet> context, ProcessorState state = null)
 {
     this.arch        = arch;
     this.segmentMap  = segmentMap;
     this.context     = context;
     this.state       = state;
     this.cmp         = new ExpressionValueComparer();
     this.memAccesses = new Dictionary <Address, DataType>();
 }
Ejemplo n.º 9
0
 public ProjectionFilter(SsaState ssa, Statement stm, SegmentedAccessClassifier sac)
 {
     this.ssa           = ssa;
     this.Statement     = stm;
     this.sac           = sac;
     this.arch          = ssa.Procedure.Architecture;
     this.NewStatements = new HashSet <Statement>();
     this.cmp           = new ExpressionValueComparer();
 }
Ejemplo n.º 10
0
 public TrashedRegisterSummarizer(IProcessorArchitecture arch, Procedure proc, ProcedureFlow pf, SymbolicEvaluationContext ctx)
 {
     this.arch = arch;
     this.proc = proc;
     this.pf   = pf;
     trashed   = new HashSet <RegisterStorage>();
     preserved = new HashSet <RegisterStorage>();
     this.ctx  = ctx;
     this.cmp  = new ExpressionValueComparer();
 }
Ejemplo n.º 11
0
 public BackwardSlicer(IBackWalkHost <RtlBlock, RtlInstruction> host, RtlBlock rtlBlock, ProcessorState state)
 {
     this.host           = host;
     this.rtlBlock       = rtlBlock;
     this.processorState = state;
     this.worklist       = new WorkList <SliceState>();
     this.visited        = new HashSet <RtlBlock>();
     this.cmp            = new ExpressionValueComparer();
     this.simp           = new ExpressionSimplifier(host.SegmentMap, new EvalCtx(state.Endianness), NullDecompilerEventListener.Instance);
 }
Ejemplo n.º 12
0
 public TrashedRegisterSummarizer(IProcessorArchitecture arch, Procedure proc, ProcedureFlow pf, SymbolicEvaluationContext ctx)
 {
     this.arch = arch;
     this.proc = proc;
     this.pf = pf;
     trashed = new HashSet<RegisterStorage>();
     preserved = new HashSet<RegisterStorage>();
     this.ctx = ctx;
     this.cmp = new ExpressionValueComparer();
 }
Ejemplo n.º 13
0
 public zSeriesRewriter(zSeriesArchitecture arch, EndianImageReader rdr, ProcessorState state, IStorageBinder binder, IRewriterHost host)
 {
     this.arch   = arch;
     this.rdr    = rdr;
     this.state  = state;
     this.binder = binder;
     this.host   = host;
     this.dasm   = new zSeriesDisassembler(arch, rdr).GetEnumerator();
     this.cmp    = new ExpressionValueComparer();
 }
Ejemplo n.º 14
0
 public MipsRewriter(MipsProcessorArchitecture arch, EndianImageReader rdr, IEnumerable <MipsInstruction> instrs, IStorageBinder binder, IRewriterHost host)
 {
     this.arch            = arch;
     this.binder          = binder;
     this.rdr             = rdr;
     this.dasm            = instrs.GetEnumerator();
     this.host            = host;
     this.cmp             = new ExpressionValueComparer();
     this.rtlInstructions = new List <RtlInstruction>();
     this.m = new RtlEmitter(rtlInstructions);
 }
Ejemplo n.º 15
0
 public TrashedRegisterFinder(
     Program program,
     IEnumerable <Procedure> procedures,
     ProgramDataFlow flow,
     DecompilerEventListener eventListener)
 {
     this.program       = program;
     this.procedures    = procedures;
     this.flow          = flow;
     this.eventListener = eventListener ?? NullDecompilerEventListener.Instance;
     this.worklist      = new WorkList <Block>();
     this.visited       = new HashSet <Block>();
     this.ecomp         = new ExpressionValueComparer();
 }
Ejemplo n.º 16
0
 private Context(
     SsaState ssa,
     Identifier fp,
     Dictionary <Identifier, Tuple <Expression, BitRange> > idState,
     ProcedureFlow procFlow,
     Dictionary <int, Expression> stack,
     ExpressionValueComparer cmp)
 {
     this.ssa          = ssa;
     this.FramePointer = fp;
     this.IdState      = idState;
     this.ProcFlow     = procFlow;
     this.StackState   = stack;
     this.cmp          = cmp;
 }
Ejemplo n.º 17
0
 public TrashedRegisterFinder(
     Program program,
     ProgramDataFlow flow,
     IEnumerable <SsaTransform> sccGroup,
     DecompilerEventListener listener)
 {
     this.arch       = program.Architecture;
     this.segmentMap = program.SegmentMap;
     this.flow       = flow;
     this.sccGroup   = sccGroup.ToHashSet();
     this.callGraph  = program.CallGraph;
     this.listener   = listener;
     this.cmp        = new ExpressionValueComparer();
     this.worklist   = new WorkStack <Block>();
     this.ssas       = sccGroup.ToDictionary(s => s.SsaState.Procedure, s => s.SsaState);
 }
Ejemplo n.º 18
0
 public ExpressionSimplifier(SegmentMap segmentMap, EvaluationContext ctx, DecompilerEventListener listener)
 {
     this.segmentMap              = segmentMap ?? throw new ArgumentNullException(nameof(SegmentMap));
     this.ctx                     = ctx;
     this.cmp                     = new ExpressionValueComparer();
     this.m                       = new ExpressionEmitter();
     this.unifier                 = new Unifier();
     this.add2ids                 = new AddTwoIdsRule(ctx);
     this.addEcc                  = new Add_e_c_cRule(ctx);
     this.addMici                 = new Add_mul_id_c_id_Rule(ctx);
     this.idConst                 = new IdConstant(ctx, unifier, listener);
     this.idCopyPropagation       = new IdCopyPropagationRule(ctx);
     this.idBinIdc                = new IdBinIdc_Rule(ctx);
     this.sliceConst              = new SliceConstant_Rule();
     this.sliceMem                = new SliceMem_Rule();
     this.sliceSegPtr             = new SliceSegmentedPointer_Rule(ctx);
     this.negSub                  = new NegSub_Rule();
     this.constConstBin           = new ConstConstBin_Rule();
     this.shAdd                   = new Shl_add_Rule(ctx);
     this.shMul                   = new Shl_mul_e_Rule(ctx);
     this.shiftShift              = new ShiftShift_c_c_Rule(ctx);
     this.mpsRule                 = new Mps_Constant_Rule(ctx);
     this.sliceShift              = new SliceShift(ctx);
     this.binopWithSelf           = new BinOpWithSelf_Rule();
     this.constDiv                = new ConstDivisionImplementedByMultiplication(ctx);
     this.idProcConstRule         = new IdProcConstRule(ctx);
     this.convertConvertRule      = new ConvertConvertRule(ctx);
     this.distributedConvert      = new DistributedConversionRule();
     this.distributedCast         = new DistributedCastRule();
     this.distributedSlice        = new DistributedSliceRule();
     this.mkSeqFromSlicesRule     = new MkSeqFromSlices_Rule(ctx);
     this.constOnLeft             = new ComparisonConstOnLeft();
     this.sliceSeq                = new SliceSequence(ctx);
     this.sliceConvert            = new SliceConvert();
     this.logicalNotFollowedByNeg = new LogicalNotFollowedByNegRule();
     this.logicalNotFromBorrow    = new LogicalNotFromArithmeticSequenceRule();
     this.unaryNegEqZero          = new UnaryNegEqZeroRule();
     this.scaledIndexRule         = new ScaledIndexRule(ctx);
 }
Ejemplo n.º 19
0
        private void CompareParameterReferences(ScriptExpression origExp, ScriptExpression modExp)
        {
            ExpressionValueComparer valComparer = new ExpressionValueComparer();

            bool areEqual = true;

            areEqual = areEqual && (origExp.Opcode == modExp.Opcode);
            areEqual = areEqual && (origExp.ReturnType == modExp.ReturnType);
            areEqual = areEqual && (origExp.StringValue == modExp.StringValue);
            areEqual = areEqual && valComparer.Equals(origExp.Value, modExp.Value);

            if (!areEqual)
            {
                WriteScriptObject();

                _output.WriteLine("Unequal Parameter References!");
                _output.WriteLine("### Original ###");
                _output.WriteLine(ExpressionToString(origExp));
                _output.WriteLine("### Modified ###");
                _output.WriteLine(ExpressionToString(modExp));
                _output.WriteLine();
            }
        }
Ejemplo n.º 20
0
 public RegisterPreservation(Dictionary<Procedure, SsaState> scc, DataFlow2 dataFlow)
 {
     this.scc = scc;
     this.dataFlow = dataFlow;
     this.cmp = new ExpressionValueComparer();
 }
Ejemplo n.º 21
0
        private void CompareNormalExpressions(ScriptExpression origExp, ScriptExpression modExp)
        {
            ExpressionValueComparer valComparer = new ExpressionValueComparer();
            bool areEqual = true;

            // the opcodes always have to match.
            areEqual = areEqual && (origExp.Opcode == modExp.Opcode);
            areEqual = areEqual && (origExp.ReturnType == modExp.ReturnType);

            // An expression's opcode determines its actual value type. The value type is used for casting. Function names are an exception.
            string valueType = _op.GetTypeInfo(origExp.ReturnType).Name == "function_name" ? "function_name" : _op.GetTypeInfo(origExp.Opcode).Name;

            switch (valueType)
            {
            case "void":
            case "boolean":
            case "long":
            case "short":
                // ignore random strings.
                areEqual = areEqual && valComparer.Equals(origExp.Value, modExp.Value);
                break;

            case "real":
                byte[] b1  = BitConverter.GetBytes(origExp.Value.UintValue);
                byte[] b2  = BitConverter.GetBytes(modExp.Value.UintValue);
                float  fl1 = BitConverter.ToSingle(b1, 0);
                float  fl2 = BitConverter.ToSingle(b2, 0);
                if ((fl1 != 0.0 || fl2 != -0.0) && (fl1 != -0.0 || fl2 != 0.0))
                {
                    areEqual = areEqual && valComparer.Equals(origExp.Value, modExp.Value);
                }
                break;

            case "string":
            case "string_id":
            case "function_name":
                areEqual = areEqual && (origExp.StringValue == modExp.StringValue);
                break;

            case "sound":
            case "effect":
            case "damage":
            case "looping_sound":
            case "animation_graph":
            case "damage_effect":
            case "object_definition":
            case "bitmap":
            case "shader":
            case "render_model":
            case "structure_definition":
            case "lightmap_definition":
            case "cinematic_definition":
            case "cinematic_scene_definition":
            case "cinematic_transition_definition":
            case "bink_definition":
            case "cui_screen_definition":
            case "any_tag":
            case "any_tag_not_resolving":
            case "ai_line":
            case "unit_seat_mapping":
                areEqual = areEqual && valComparer.Equals(origExp.Value, modExp.Value);
                // ignore missing tags, ai lines and unit seat mappings
                if (!origExp.Value.IsNull)
                {
                    areEqual = areEqual && (origExp.StringValue == modExp.StringValue);
                }
                break;

            default:
                areEqual = areEqual && valComparer.Equals(origExp.Value, modExp.Value);
                // Ignore enum values, where a space char was replaced with an underscore.
                if (origExp.StringValue != modExp.StringValue && (!_op.GetTypeInfo(valueType).IsEnum || origExp.StringValue.Replace(' ', '_') != modExp.StringValue))
                {
                    areEqual = false;
                }
                break;
            }

            if (!areEqual)
            {
                WriteScriptObject();

                _output.WriteLine("Unequal Expressions!");
                _output.WriteLine("### Original ###");
                _output.WriteLine(ExpressionToString(origExp));
                _output.WriteLine("### Modified ###");
                _output.WriteLine(ExpressionToString(modExp));
                _output.WriteLine();
            }
        }
Ejemplo n.º 22
0
 public ForLoopRewriter(Procedure proc)
 {
     this.proc = proc;
     this.cmp  = new ExpressionValueComparer();
 }
Ejemplo n.º 23
0
 public ProcedurePrettifier(Procedure proc)
 {
     this.proc = proc;
     this.cmp  = new ExpressionValueComparer();
 }
Ejemplo n.º 24
0
 public RegisterPreservation(Dictionary <Procedure, SsaState> scc, DataFlow2 dataFlow)
 {
     this.scc      = scc;
     this.dataFlow = dataFlow;
     this.cmp      = new ExpressionValueComparer();
 }
Ejemplo n.º 25
0
 public AdjacentBranchCollector(Procedure proc, DecompilerEventListener listener)
 {
     this.proc     = proc;
     this.listener = listener;
     this.cmp      = new ExpressionValueComparer();
 }