summaryrefslogtreecommitdiff
path: root/Source/GPUVerify/WriteCollector.cs
blob: 00ca63c5e1597557b3232d23021ad2abb8614491 (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
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Boogie;
using Microsoft.Basetypes;

namespace GPUVerify
{
    class WriteCollector : AccessCollector
    {

        private AccessRecord access = null;

        public WriteCollector(IKernelArrayInfo NonLocalState)
            : base(NonLocalState)
        {
        }

        public override AssignLhs VisitSimpleAssignLhs(SimpleAssignLhs node)
        {
            Debug.Assert(NoWrittenVariable());
            if (NonLocalState.Contains(node.DeepAssignedVariable))
            {
                access = new AccessRecord(node.DeepAssignedVariable, null, null, null);
            }
            return node;
        }

        private bool NoWrittenVariable()
        {
            return access == null;
        }

        public override AssignLhs VisitMapAssignLhs(MapAssignLhs node)
        {
            Debug.Assert(NoWrittenVariable());

            if (!NonLocalState.Contains(node.DeepAssignedVariable))
            {
                return node;
            }

            Variable WrittenVariable = node.DeepAssignedVariable;

            MapAssignLhs MapAssignX = node;

            CheckMapIndex(MapAssignX);
            Expr IndexX = MapAssignX.Indexes[0];
            Expr IndexY = null;
            Expr IndexZ = null;

            if (MapAssignX.Map is MapAssignLhs)
            {
                MapAssignLhs MapAssignY = MapAssignX.Map as MapAssignLhs;
                CheckMapIndex(MapAssignY);
                IndexY = MapAssignY.Indexes[0];
                if (MapAssignY.Map is MapAssignLhs)
                {
                    MapAssignLhs MapAssignZ = MapAssignY.Map as MapAssignLhs;
                    CheckMapIndex(MapAssignZ);
                    IndexZ = MapAssignZ.Indexes[0];
                    if (!(MapAssignZ.Map is SimpleAssignLhs))
                    {
                        Console.WriteLine("*** Error - maps with more than three levels of nesting are not supported");
                        Environment.Exit(1);
                    }
                }
                else
                {
                    Debug.Assert(MapAssignY.Map is SimpleAssignLhs);
                }
            }
            else
            {
                Debug.Assert(MapAssignX.Map is SimpleAssignLhs);
            }

            access = new AccessRecord(WrittenVariable, IndexZ, IndexY, IndexX);

            return MapAssignX;
        }

        private void CheckMapIndex(MapAssignLhs node)
        {
            if (node.Indexes.Count > 1)
            {
                MultiDimensionalMapError();
            }
        }




        internal bool FoundWrite()
        {
            return access != null;
        }

        internal AccessRecord GetAccess()
        {
            return access;
        }

    }
}