Beispiel #1
0
        //Create LoopControl SubExpressionGraph and remember End's Vertex in this loop control, in while this End will be changed acording break or continue label
        public override SubExpressionGraph Visit(LoopControlNode node)
        {
            var sub_graph = new SubExpressionGraph();

            if (!ReferenceEquals(node.Value, null))
            {
                node.Value = ProcessSingleNode(node.Value, ref sub_graph);
            }

            if (node.Depth > while_stack.Count)
            {
                throw new Exception("Not enough whiles on stack.");
            }
            WhileNode loop_control_while = while_stack[while_stack.Count - node.Depth - 1];

            SubExpressionGraph loop_control_graph  = SubExpressionGraph.CreateOneVertexSubGraph(node.Value);
            OneJumpVertex      loop_control_vertex = (OneJumpVertex)loop_control_graph.Start;

            if (while_for_loop_control.ContainsKey(node.Mode) && while_for_loop_control[node.Mode].ContainsKey(loop_control_while))
            {
                while_for_loop_control[node.Mode][loop_control_while].Add(loop_control_vertex);
            }
            else
            {
                while_for_loop_control[node.Mode][loop_control_while] = new List <Vertex> {
                    loop_control_vertex
                }
            };

            sub_graph = SubExpressionGraph.ConcatSubGraph(sub_graph, loop_control_graph);
            sub_graph.ContainsExtracted = true;

            return(sub_graph);
        }
 public override void Visit(BlockExpressionNode node)
 {
     base.Visit(node);
     node.Elements = ReplaceChangedNodes(node.Elements);
     foreach (var expression in node.Elements)
     {
         OneJumpVertex next_vertex = new OneJumpVertex(null, expression);
         graph_vertex.Add(next_vertex);
         AddNextJump(next_vertex);
         nodes_to_next_jmp.Add(next_vertex);
     }
 }
        public override void Visit(WhileNode node)
        {
            while_stack.Add(node);

            List <Vertex> state_before = nodes_to_next_jmp;

            base.Visit(node);

            if (changed_nodes.ContainsKey(node.Condition))
            {
                node.Condition = changed_nodes[node.Condition];
            }
            if (changed_nodes.ContainsKey(node.Body))
            {
                node.Body = changed_nodes[node.Body];
            }
            if (changed_nodes.ContainsKey(node.Else))
            {
                node.Else = changed_nodes[node.Else];
            }

            ConditionalJumpVertex condition_vertex = new ConditionalJumpVertex(null, null, node.Condition);

            graph_vertex.Add(condition_vertex);
            AddNextJump(condition_vertex);

            //TODO try to find begin before while

            OneJumpVertex body_vertex = new OneJumpVertex(condition_vertex, node.Body);

            graph_vertex.Add(body_vertex);

            condition_vertex.TrueJump = body_vertex;
            nodes_to_next_jmp.Add(condition_vertex);

            if (while_for_loop_control.ContainsKey(LoopControlMode.LCM_BREAK) && while_for_loop_control[LoopControlMode.LCM_BREAK].ContainsKey(node))
            {
                foreach (var loop_control_vetex in while_for_loop_control[LoopControlMode.LCM_BREAK][node])
                {
                    nodes_to_next_jmp.Add(loop_control_vetex);
                }
            }
            if (while_for_loop_control.ContainsKey(LoopControlMode.LCM_CONTINUE) && while_for_loop_control[LoopControlMode.LCM_CONTINUE].ContainsKey(node))
            {
                foreach (var loop_control_vetex in while_for_loop_control[LoopControlMode.LCM_CONTINUE][node])
                {
                    ((OneJumpVertex)loop_control_vetex).Jump = body_vertex;
                }
            }
            while_stack.Remove(node);
        }
        public override void Visit(LoopControlNode node)
        {
            base.Visit(node);

            if (node.Depth > while_stack.Count)
            {
                throw new Exception("Not enough whiles on stack.");
            }
            WhileNode loop_control_while = while_stack[while_stack.Count - node.Depth - 1];

            OneJumpVertex loop_control_vertex = new OneJumpVertex(null, node.Value);

            if (while_for_loop_control.ContainsKey(node.Mode) && while_for_loop_control[node.Mode].ContainsKey(loop_control_while))
            {
                while_for_loop_control[node.Mode][loop_control_while].Add(loop_control_vertex);
            }
            else
            {
                while_for_loop_control[node.Mode][loop_control_while] = new List <Vertex> {
                    loop_control_vertex
                }
            };
        }
Beispiel #5
0
        //create cycle for while
        public override SubExpressionGraph Visit(WhileNode node)
        {
            while_stack.Add(node);

            var sub_graph = new SubExpressionGraph();

            var begin_while = SubExpressionGraph.CreateOneVertexSubGraph(new BlockExpressionNode());

            if (!ReferenceEquals(node.Condition, null))
            {
                node.Condition = ProcessSingleNode(node.Condition, ref sub_graph);
            }

            if (!ReferenceEquals(node.Body, null))
            {
                node.Body = ProcessSingleNode(node.Body, ref sub_graph);
            }

            if (node.HasElse)
            {
                node.Else = ProcessSingleNode(node.Else, ref sub_graph);
            }

            VariableDefNode t = new VariableDefNode();

            t.ExpressionType = node.ExpressionType;
            t.Name           = "T" + temporary_counter;
            AtomNode _0 = new AtomNode(node.ExpressionType);

            _0.Value = "";
            t.Value  = _0;
            VariableUseNode t_use = new VariableUseNode();

            t_use.ExpressionType = node.ExpressionType;
            t_use.Declaration    = t;
            t_use.Name           = "T" + temporary_counter;

            List <Vertex> while_graph_vertex = new List <Vertex>();

            OperatorNode assign_body = new OperatorNode();

            assign_body.Operator  = OperatorType.ASSIGN;
            assign_body.Arguments = new List <ExpressionNode> {
                t_use, node.Body
            };

            ConditionalJumpVertex condition_vertex = new ConditionalJumpVertex(null, null, node.Condition);

            while_graph_vertex.Add(condition_vertex);

            OneJumpVertex body_vertex = new OneJumpVertex(condition_vertex, assign_body);

            while_graph_vertex.Add(body_vertex);

            OneJumpVertex end_while_vertex = new OneJumpVertex(null, t_use);

            condition_vertex.TrueJump = body_vertex;
            body_vertex.Jump          = begin_while.Start;

            if (node.HasElse)
            {
                OperatorNode assign_else = new OperatorNode();
                assign_else.Operator  = OperatorType.ASSIGN;
                assign_else.Arguments = new List <ExpressionNode> {
                    t_use, node.Else
                };

                OneJumpVertex else_vertex = new OneJumpVertex(end_while_vertex, assign_else);
                while_graph_vertex.Add(else_vertex);
                condition_vertex.FalseJump = else_vertex;
            }
            else
            {
                condition_vertex.FalseJump = end_while_vertex;
            }

            while_graph_vertex.Add(end_while_vertex);

            sub_graph = SubExpressionGraph.ConcatSubGraph(sub_graph, new SubExpressionGraph(condition_vertex, while_graph_vertex, end_while_vertex, true, null));

            sub_graph.Temporary = t_use;
            temporary_counter++;

            if (while_for_loop_control.ContainsKey(LoopControlMode.LCM_BREAK) && while_for_loop_control[LoopControlMode.LCM_BREAK].ContainsKey(node))
            {
                foreach (var loop_control_vertex in while_for_loop_control[LoopControlMode.LCM_BREAK][node])
                {
                    ((OneJumpVertex)loop_control_vertex).Jump = sub_graph.End;
                }
            }
            if (while_for_loop_control.ContainsKey(LoopControlMode.LCM_CONTINUE) && while_for_loop_control[LoopControlMode.LCM_CONTINUE].ContainsKey(node))
            {
                foreach (var loop_control_vertex in while_for_loop_control[LoopControlMode.LCM_CONTINUE][node])
                {
                    ((OneJumpVertex)loop_control_vertex).Jump = sub_graph.Start;
                }
            }

            while_stack.Remove(node);

            return(sub_graph);
        }
Beispiel #6
0
        //Create diamond graph with CondtionVertex, mark flag IF
        public override SubExpressionGraph Visit(IfNode node)
        {
            var sub_graph = new SubExpressionGraph();

            if (!ReferenceEquals(node.Condition, null))
            {
                node.Condition = ProcessSingleNode(node.Condition, ref sub_graph);
            }

            if (!ReferenceEquals(node.Then, null))
            {
                node.Then = ProcessSingleNode(node.Then, ref sub_graph);
            }

            if (node.HasElse)
            {
                node.Else = ProcessSingleNode(node.Else, ref sub_graph);
            }

            VariableDefNode t = new VariableDefNode();

            t.ExpressionType = node.ExpressionType;
            t.Name           = "T" + temporary_counter;
            AtomNode _0 = new AtomNode(node.ExpressionType);

            _0.Value = "";
            t.Value  = _0;
            VariableUseNode t_use = new VariableUseNode();

            t_use.ExpressionType = node.ExpressionType;
            t_use.Declaration    = t;
            t_use.Name           = "T" + temporary_counter;

            OperatorNode assign_then = new OperatorNode();

            assign_then.Operator  = OperatorType.ASSIGN;
            assign_then.Arguments = new List <ExpressionNode> {
                t_use, node.Then
            };

            OneJumpVertex         then_vertex      = new OneJumpVertex(null, assign_then);
            OneJumpVertex         end_if           = new OneJumpVertex(null, t_use);
            ConditionalJumpVertex condition_vertex = new ConditionalJumpVertex(then_vertex, end_if, node.Condition);

            var vertex_in_if = new List <Vertex> {
                condition_vertex, then_vertex
            };

            if (node.HasElse)
            {
                OperatorNode assign_else = new OperatorNode();
                assign_else.Operator  = OperatorType.ASSIGN;
                assign_else.Arguments = new List <ExpressionNode> {
                    t_use, node.Else
                };

                OneJumpVertex else_vertex = new OneJumpVertex(null, assign_else);

                condition_vertex.FalseJump = else_vertex;
                else_vertex.Jump           = end_if;

                vertex_in_if.Add(else_vertex);
            }
            else
            {
                condition_vertex.FalseJump = end_if;
            }

            vertex_in_if.Add(end_if);

            var if_sub_graph = new SubExpressionGraph(condition_vertex, vertex_in_if, end_if, true, null);

            sub_graph = SubExpressionGraph.ConcatSubGraph(sub_graph, if_sub_graph);

            sub_graph.Temporary = t_use;
            temporary_counter++;

            return(sub_graph);
        }
        public override void Visit(IfNode node)
        {
            base.Visit(node);

            if (changed_nodes.ContainsKey(node.Condition))
            {
                node.Condition = changed_nodes[node.Condition];
            }
            if (changed_nodes.ContainsKey(node.Then))
            {
                node.Then = changed_nodes[node.Then];
            }
            if (changed_nodes.ContainsKey(node.Else))
            {
                node.Else = changed_nodes[node.Else];
            }

            VariableDefNode t = new VariableDefNode();

            t.Name = "T" + temporary_counter;
            AtomNode _0 = new AtomNode(NamedTypeNode.IntType());

            _0.Value = "0";
            t.Value  = _0;
            VariableUseNode t_use = new VariableUseNode();

            t_use.Declaration = t;
            t_use.Name        = "T" + temporary_counter;

            OperatorNode assign_then = new OperatorNode();

            assign_then.Operator  = OperatorType.ASSIGN;
            assign_then.Arguments = new List <ExpressionNode> {
                t_use, node.Then
            };
            OneJumpVertex then_vertex = new OneJumpVertex(null, assign_then);

            graph_vertex.Add(then_vertex);

            ConditionalJumpVertex condition_vertex = new ConditionalJumpVertex(then_vertex, null, node.Condition);

            graph_vertex.Add(condition_vertex);
            AddNextJump(condition_vertex);

            nodes_to_next_jmp.Add(then_vertex);

            if (node.HasElse)
            {
                OperatorNode assign_else = new OperatorNode();
                assign_else.Operator  = OperatorType.ASSIGN;
                assign_else.Arguments = new List <ExpressionNode> {
                    t_use, node.Else
                };
                OneJumpVertex else_vertex = new OneJumpVertex(null, assign_else);
                graph_vertex.Add(else_vertex);
                nodes_to_next_jmp.Add(else_vertex);
                condition_vertex.FalseJump = else_vertex;
            }
            else
            {
                nodes_to_next_jmp.Add(condition_vertex);
            }

            changed_nodes[node] = t_use;
            temporary_counter++;
        }