Exemplo n.º 1
0
        protected bool ValidateOperand(InstructionNode node, int operandIndex, ExpressionVariables variables)
        {
            if (operandIndex > node.OperandCount)
            {
                return(false);
            }

            return(ValidateOperand(node.GetOperand(operandIndex), variables));
        }
Exemplo n.º 2
0
        public Node Transform(InstructionNode node, TypeSystem typeSystem, ExpressionVariables variables)
        {
            if (!Validate(node, variables))
            {
                return(null);
            }

            return(Transform(Result, node.Block, typeSystem, variables));
        }
        protected static Value Method(ExpressionNode node, ExpressionVariables variables)
        {
            string name = node.Token.Value;

            //result = BuiltInMethods.Evaluate(name, node.Parameters);

            //if (result != null)
            //	return result;

            throw new CompilerException("ExpressionEvaluation: unknown method: " + name);
        }
        protected static Value IfStatement(ExpressionNode node, ExpressionVariables variables)
        {
            if (node.Parameters.Count < 2 || node.Parameters.Count > 3)
            {
                throw new CompilerException("ExpressionEvaluation: Incomplete if statement");
            }

            var condition = Evaluate(node.Parameters[0], variables);

            return(Evaluate(node.Parameters[condition.IsTrue ? 1 : 2], variables));
        }
        public static Value Evaluate(ExpressionNode root, ExpressionVariables variables)
        {
            Debug.Assert(root != null);

            if (root.Left != null && root.Right != null)
            {
                var left = Evaluate(root.Left, variables);

                // shortcut evaluation
                if (root.Token.TokenType == TokenType.And && left.IsFalse)
                {
                    return(left);
                }
                else if (root.Token.TokenType == TokenType.Or && left.IsTrue)
                {
                    return(left);
                }

                var right = Evaluate(root.Right, variables);

                return(Eval(root.Token.TokenType, left, right));
            }
            else if (root.Left != null && root.Right == null)
            {
                var left = Evaluate(root.Left, variables);

                return(Eval(root.Token.TokenType, left));
            }
            else if (root.Left == null && root.Right == null)
            {
                if (root.Token.TokenType == TokenType.If)
                {
                    return(IfStatement(root, variables));
                }
                else if (root.Token.TokenType == TokenType.OperandVariable)
                {
                    return(OperandVariable(root));
                }
                else if (root.Token.TokenType == TokenType.Method)
                {
                    return(Method(root, variables));
                }
                else
                {
                    // must be a literal
                    return(EvalLiteral(root.Token));
                }
            }

            throw new CompilerException("ExpressionEvaluation: unexpected token type: " + root.Token);
        }
Exemplo n.º 6
0
        protected Node Transform(Node node, BasicBlock block, TypeSystem typeSystem, ExpressionVariables variables)
        {
            var newNode = new InstructionNode(node.Instruction, block);

            // todo: determine result type
            // todo: determine size

            int operandIndex = 0;

            foreach (var nodeOperand in node.ParentNodes)
            {
                if (nodeOperand.NodeType == NodeType.Expression)
                {
                    // todo: expressions
                }
                else if (nodeOperand.NodeType == NodeType.FixedIntegerConstant)
                {
                    // todo
                }
                else if (nodeOperand.NodeType == NodeType.FixedDoubleConstant)
                {
                    // todo
                }
                else if (nodeOperand.NodeType == NodeType.ConstantVariable)
                {
                    newNode.SetOperand(operandIndex++, variables.GetOperand(nodeOperand.Alias));
                    continue;
                }
                else if (nodeOperand.NodeType == NodeType.PhyiscalRegister)
                {
                    var type = typeSystem.BuiltIn.I4;                       // todo
                    Operand.CreateCPURegister(type, node.PhysicalRegister);
                    continue;
                }
                else if (nodeOperand.NodeType == NodeType.VirtualRegister)
                {
                    newNode.SetOperand(operandIndex++, variables.GetOperand(nodeOperand.Alias));
                    continue;
                }
                else if (nodeOperand.NodeType == NodeType.OperandVariable)
                {
                    newNode.SetOperand(operandIndex++, variables.GetOperand(nodeOperand.Alias));
                    continue;
                }

                throw new CompilerException("Error");
            }

            return(null);
        }
Exemplo n.º 7
0
        public bool Match(InstructionNode node, ExpressionVariables variables)
        {
            if (NodeType == NodeType.Instruction)
            {
                if (!ValidateInstruction(node))
                {
                    return(false);
                }

                for (int i = 0; i < ParentNodes.Count; i++)
                {
                    var parentNode = ParentNodes[i];

                    if (NodeType == NodeType.Any)
                    {
                        continue;
                    }

                    if (parentNode.NodeType == NodeType.Instruction)
                    {
                        var operand = node.GetOperand(i);

                        if (operand.Definitions.Count != 1)
                        {
                            return(false);
                        }

                        var parent = operand.Definitions[0];

                        if (!parentNode.Match(parent, variables))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (!parentNode.ValidateOperand(node, i, variables))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 8
0
        public bool Validate(InstructionNode node, ExpressionVariables variables)
        {
            // validate the instructions and operands match
            bool match = Match.Match(node, variables);

            if (!match)
            {
                return(false);
            }

            // validate against any criteria
            if (Condition == null)
            {
                return(true);
            }

            var result = ExpressionEvaluator.Evaluate(Condition, variables);

            return(result.IsTrue);
        }
Exemplo n.º 9
0
        public bool Validate(InstructionNode node)
        {
            var variables = new ExpressionVariables();

            return(Validate(node, variables));
        }
Exemplo n.º 10
0
        protected bool ValidateOperand(Operand operand, ExpressionVariables variables)
        {
            if (operand == null)
            {
                return(false);
            }

            if (NodeType == NodeType.Instruction)
            {
                return(false);
            }

            if (NodeType == NodeType.PhyiscalRegister && operand.IsCPURegister && operand.Register == PhysicalRegister)
            {
                return(true);
            }

            if (NodeType == NodeType.VirtualRegister && operand.IsVirtualRegister)
            {
                return(true);
            }

            if (NodeType == NodeType.FixedIntegerConstant && operand.IsResolvedConstant && operand.ConstantUnsignedLongInteger == ConstantInteger)
            {
                return(true);
            }

            if (NodeType == NodeType.ConstantVariable && operand.IsConstant)
            {
                var variableOperand = variables.GetOperand(Alias);

                if (variableOperand == null)
                {
                    variables.SetOperand(Alias, operand);
                    return(true);
                }
                else
                {
                    return(variableOperand.ConstantUnsignedInteger == operand.ConstantUnsignedInteger);
                }
            }

            if (NodeType == NodeType.OperandVariable)
            {
                var variableOperand = variables.GetOperand(Alias);

                if (variableOperand == null)
                {
                    variables.SetOperand(Alias, operand);
                    return(true);
                }
                else
                {
                    return(variableOperand == operand);
                }
            }

            if (NodeType == NodeType.TypeVariable)
            {
                var variableType = variables.GetType(Alias);

                if (variableType == null)
                {
                    variables.SetType(Alias, operand.Type);
                    return(true);
                }
                else
                {
                    return(variableType == operand.Type);
                }
            }

            return(false);
        }