summaryrefslogtreecommitdiff
path: root/Source/GPUVerify/NonLocalAccessExtractor.cs
blob: 553be6834234fe1752e0793b8624ea71088c6c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Boogie;
using System.Diagnostics;

namespace GPUVerify
{
    class NonLocalAccessExtractor : StandardVisitor
    {
        private int TempId;
        public AssignCmd Assignment = null;
        public LocalVariable Declaration = null;
        public bool done = false;

        private INonLocalState NonLocalState;

        public NonLocalAccessExtractor(int TempId, INonLocalState NonLocalState)
        {
            this.TempId = TempId;
            this.NonLocalState = NonLocalState;
        }


        public override Expr VisitNAryExpr(NAryExpr node)
        {
            if (done)
            {
                return node;
            }

            if (!NonLocalAccessCollector.IsNonLocalAccess(node, NonLocalState))
            {
                return base.VisitNAryExpr(node);
            }

            Expr temp = node;
            while (temp is NAryExpr && (temp as NAryExpr).Fun is MapSelect)
            {
                Debug.Assert((temp as NAryExpr).Args.Length == 2);

                if (NonLocalAccessCollector.ContainsNonLocalAccess((temp as NAryExpr).Args[1], NonLocalState))
                {
                    return VisitExpr((temp as NAryExpr).Args[1]);
                }
                temp = (temp as NAryExpr).Args[0];
            }

            // If we get here, we've established that the node contains no non-local accessing sub-expressions
            Debug.Assert(temp is IdentifierExpr);
            done = true;

            Declaration = new LocalVariable(node.tok, new TypedIdent(node.tok, "_temp" + TempId, node.Type));

            SimpleAssignLhs lhs = new SimpleAssignLhs(node.tok, new IdentifierExpr(node.tok, Declaration));
            Expr rhs = node;

            List<AssignLhs> lhss = new List<AssignLhs>();
            lhss.Add(lhs);

            List<Expr> rhss = new List<Expr>();
            rhss.Add(rhs);

            Assignment = new AssignCmd(node.tok, lhss, rhss);

            return new IdentifierExpr(node.tok, Declaration);
        
        }

        public override Expr VisitIdentifierExpr(IdentifierExpr node)
        {
            if (done)
            {
                return node;
            }

            if (NonLocalAccessCollector.IsNonLocalAccess(node, NonLocalState))
            {
                done = true;
                Declaration = new LocalVariable(node.tok, new TypedIdent(node.tok, "_temp" + TempId, node.Decl.TypedIdent.Type));
                
                SimpleAssignLhs lhs = new SimpleAssignLhs(node.tok, new IdentifierExpr(node.tok, Declaration));
                Expr rhs = node;

                List<AssignLhs> lhss = new List<AssignLhs>();
                lhss.Add(lhs);

                List<Expr> rhss = new List<Expr>();
                rhss.Add(rhs);

                Assignment = new AssignCmd(node.tok, lhss, rhss);

                return new IdentifierExpr(node.tok, Declaration);
            }
            return base.VisitIdentifierExpr(node);
        }


    }
}