public void Exs_OrWithSelf()
 {
     BuildExpressionSimplifier();
     var expr = new BinaryExpression(Operator.Or, foo.DataType, foo, foo);
     var result = expr.Accept(simplifier);
     Assert.AreSame(foo, result);
 }
Example #2
0
            private bool CollectInternal(BinaryExpression binaryExpression, out IList <string> foundConstants)
            {
                _literals         = foundConstants = new List <string>();
                hasNonConstInCase = false;

                binaryExpression.Accept(this);
                return(_instance.HasNonConstInCase);
            }
Example #3
0
        public void Exs_OrWithSelf()
        {
            Given_ExpressionSimplifier();
            var expr   = new BinaryExpression(Operator.Or, foo.DataType, foo, foo);
            var result = expr.Accept(simplifier);

            Assert.AreSame(foo, result);
        }
Example #4
0
        public void TestBinaryExpressionAccept()
        {
            Mock <KoraliumSqlVisitor> mock             = new Mock <KoraliumSqlVisitor>();
            BinaryExpression          binaryExpression = new BinaryExpression();

            binaryExpression.Accept(mock.Object);
            mock.Verify(x => x.VisitBinaryExpression(binaryExpression));
        }
Example #5
0
        public void Exs_Constants()
        {
            Given_ExpressionSimplifier();
            Expression expr = new BinaryExpression(Operator.IAdd, PrimitiveType.Word32,
                                                   Constant.Word32(1), Constant.Word32(2));
            Constant c = (Constant)expr.Accept(simplifier);

            Assert.AreEqual(3, c.ToInt32());
        }
		public void ExsConstants()
		{
			BuildExpressionSimplifier();
			Expression expr = new BinaryExpression(Operator.IAdd, PrimitiveType.Word32, 
				Constant.Word32(1), Constant.Word32(2));
			Constant c = (Constant) expr.Accept(simplifier);

			Assert.AreEqual(3, c.ToInt32());
		}
Example #7
0
        public void VpAddZero()
        {
            Identifier r = Reg32("r");
            Identifier s = Reg32("s");

            var sub = new BinaryExpression(Operator.ISub, PrimitiveType.Word32, new MemoryAccess(MemoryIdentifier.GlobalMemory, r, PrimitiveType.Word32), Constant.Word32(0));
            var vp = new ExpressionSimplifier(new SsaEvaluationContext(arch, ssaIds));
            var exp = sub.Accept(vp);
            Assert.AreEqual("Mem0[r:word32]", exp.ToString());
        }
Example #8
0
        public void VpAddZero()
        {
            Identifier r = m.Reg32("r");

            var sub = new BinaryExpression(Operator.ISub, PrimitiveType.Word32, new MemoryAccess(MemoryIdentifier.GlobalMemory, r, PrimitiveType.Word32), Constant.Word32(0));
            var vp  = new ExpressionSimplifier(segmentMap, new SsaEvaluationContext(arch.Object, m.Ssa.Identifiers, dynamicLinker.Object), listener);
            var exp = sub.Accept(vp);

            Assert.AreEqual("Mem0[r:word32]", exp.ToString());
        }
Example #9
0
        public void VpAddZero()
        {
            Identifier r = Reg32("r");
            Identifier s = Reg32("s");

            var sub = new BinaryExpression(Operator.ISub, PrimitiveType.Word32, new MemoryAccess(MemoryIdentifier.GlobalMemory, r, PrimitiveType.Word32), Constant.Word32(0));
            var vp  = new ExpressionSimplifier(new SsaEvaluationContext(arch, ssaIds));
            var exp = sub.Accept(vp);

            Assert.AreEqual("Mem0[r:word32]", exp.ToString());
        }
Example #10
0
		public void CfAddMul()
		{
			Identifier id1 = new Identifier("v1", PrimitiveType.Word16, null);
			Identifier id2 = new Identifier("v2", PrimitiveType.Word16, null);

			Expression e = new BinaryExpression(
				Operator.IAdd, PrimitiveType.Word16, new BinaryExpression(
				Operator.IMul, PrimitiveType.Word16, id1, id2), Constant.Word16(2));
			e.Accept(cf);

			Assert.AreEqual("v1 * v2 + 0x0002", sw.ToString());
		}
Example #11
0
        private bool OnAfterCall(FunctionType sigCallee, ProcedureCharacteristics characteristics)
        {
            UserCallData userCall = null;

            if (program.User.Calls.TryGetUpperBound(ric.Address, out userCall))
            {
                var linStart    = ric.Address.ToLinear();
                var linEnd      = linStart + ric.Length;
                var linUserCall = userCall.Address.ToLinear();
                if (linStart > linUserCall || linUserCall >= linEnd)
                {
                    userCall = null;
                }
            }
            if ((characteristics != null && characteristics.Terminates) ||
                (userCall != null && userCall.NoReturn))
            {
                scanner.TerminateBlock(blockCur, ric.Address + ric.Length);
                return(false);
            }

            if (sigCallee != null)
            {
                if (sigCallee.StackDelta != 0)
                {
                    Expression newVal = new BinaryExpression(
                        Operator.IAdd,
                        stackReg.DataType,
                        stackReg,
                        Constant.Create(
                            PrimitiveType.CreateWord(stackReg.DataType.Size),
                            sigCallee.StackDelta));
                    newVal = newVal.Accept(eval);
                    SetValue(stackReg, newVal);
                }
            }
            state.OnAfterCall(sigCallee);

            // Adjust stack after call
            if (sigCallee != null)
            {
                int delta = sigCallee.StackDelta - sigCallee.ReturnAddressOnStack;
                if (delta != 0)
                {
                    var d = Constant.Create(stackReg.DataType, delta);
                    this.Emit(new Assignment(
                                  stackReg,
                                  new BinaryExpression(Operator.IAdd, stackReg.DataType, stackReg, d)));
                }
            }
            return(true);
        }
Example #12
0
        public void CfAddMul()
        {
            Identifier id1 = new Identifier("v1", PrimitiveType.Word16, null);
            Identifier id2 = new Identifier("v2", PrimitiveType.Word16, null);

            Expression e = new BinaryExpression(
                Operator.IAdd, PrimitiveType.Word16, new BinaryExpression(
                    Operator.IMul, PrimitiveType.Word16, id1, id2), Constant.Word16(2));

            e.Accept(cf);

            Assert.AreEqual("v1 * v2 + 0x0002", sw.ToString());
        }
 public void ExsAddPositiveConstantToNegative()
 {
     BuildExpressionSimplifier();
     var expr = new BinaryExpression(
         Operator.IAdd,
         foo.DataType,
         new BinaryExpression(
             Operator.ISub,
             foo.DataType,
             foo,
             Constant.Word32(4)),
         Constant.Word32(1));
     var result = expr.Accept(simplifier);
     Assert.AreEqual("foo_0 - 0x00000003", result.ToString());
 }
Example #14
0
        public void Exs_AddPositiveConstantToNegative()
        {
            Given_ExpressionSimplifier();
            var expr = new BinaryExpression(
                Operator.IAdd,
                foo.DataType,
                new BinaryExpression(
                    Operator.ISub,
                    foo.DataType,
                    foo,
                    Constant.Word32(4)),
                Constant.Word32(1));
            var result = expr.Accept(simplifier);

            Assert.AreEqual("foo_0 - 0x00000003", result.ToString());
        }
Example #15
0
        /// <summary>
        /// VisitBinaryExpression method could not simplify following statements:
        ///    y = x - const
        ///    a = y + const
        ///    x = phi(a, b)
        /// to
        ///    y = x - const
        ///    a = x
        ///    x = phi(a, b)
        /// IdBinIdc rule class processes y as 'used in phi' and prevents propagation.
        /// This method could be used to do such simplification (y + const ==> x)
        /// </summary
        private Expression SimplifyPhiArg(Expression arg)
        {
            if (!(arg is BinaryExpression bin &&
                  bin.Left is Identifier idLeft &&
                  ctx.GetValue(idLeft) is BinaryExpression binLeft))
            {
                return(arg);
            }

            ctx.RemoveIdentifierUse(idLeft);
            ctx.UseExpression(binLeft);
            bin = new BinaryExpression(
                bin.Operator,
                bin.DataType,
                binLeft,
                bin.Right);
            return(bin.Accept(this));
        }
Example #16
0
 private CallSite OnBeforeCall(Identifier stackReg, int sizeOfRetAddrOnStack)
 {
     if (sizeOfRetAddrOnStack > 0)
     {
         //$BUG: stack grows negative here; some stacks might grow
         // positive?
         Expression newVal = new BinaryExpression(
             Operator.ISub,
             stackReg.DataType,
             stackReg,
             Constant.Create(
                 PrimitiveType.CreateWord(sizeOfRetAddrOnStack),
                 sizeOfRetAddrOnStack));
         newVal = newVal.Accept(eval);
         SetValue(stackReg, newVal);
     }
     return(state.OnBeforeCall(stackReg, sizeOfRetAddrOnStack));
 }
Example #17
0
        /// <summary>
        /// VisitBinaryExpression method could not simplify following statements:
        ///    y = x - const
        ///    a = y + const
        ///    x = phi(a, b)
        /// to
        ///    y = x - const
        ///    a = x
        ///    x = phi(a, b)
        /// IdBinIdc rule class processes y as 'used in phi' and prevents propagation.
        /// This method could be used to do such simplification (y + const ==> x)
        /// </summary
        private Expression SimplifyPhiArg(Expression arg)
        {
            BinaryExpression bin, binLeft;
            Identifier       idLeft;

            if (
                !arg.As(out bin) || !bin.Left.As(out idLeft) ||
                !ctx.GetValue(idLeft).As(out binLeft)
                )
            {
                return(arg);
            }
            ctx.RemoveIdentifierUse(idLeft);
            ctx.UseExpression(binLeft);
            bin = new BinaryExpression(
                bin.Operator,
                bin.DataType,
                binLeft,
                bin.Right);
            return(bin.Accept(this));
        }
            public override void VisitBinaryExpression(BinaryExpression node)
            {
                var opKind = GetOperatorKind(node.Kind);

                switch (opKind)
                {
                case OperatorKind.And:
                case OperatorKind.Or:
                    // stay in special rules for and/or
                    node.Left.Accept(this);
                    node.Right.Accept(this);
                    var info = _binder.GetBinaryOperatorInfo(opKind, node.Left, node.Right, node.Operator);
                    _binder.SetSemanticInfo(node, info);
                    break;

                default:
                    // drop back to normal expression binding for anything else
                    node.Accept(_treeBinder);
                    break;
                }
            }
        public override void OnBinaryExpression(BinaryExpression node)
        {
            if (node.ParentNode != _owner)
            {
                return;
            }

            switch (node.Left.NodeType)
            {
            case NodeType.ReferenceExpression:
                var declaration = new Declaration(VariableNameFor((ReferenceExpression)node.Left), CodeBuilder.CreateTypeReference(node.ExpressionType));
                _statements.Add(new DeclarationStatement(declaration, node.Right));
                node.Right.Accept(ReferenceNameFix.Instance);
                break;

            case NodeType.MemberReferenceExpression:
                _statements.Add(new ExpressionStatement(node));
                node.Accept(ReferenceNameFix.Instance);
                break;
            }
        }
Example #20
0
        public void VisitorWithReturnIsImplemented()
        {
            var sut = new BinaryExpression();

            sut.Accept(23).VerifyWithReturn(v => v.Visit(sut, 23));
        }
Example #21
0
 private void FixNonConstReferencesAsCaseCondition(BinaryExpression condition, Expression tbc)
 {
     condition.Accept(new NonConstSwitchConditionFixer(tbc));
 }