aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/IntermediateRepresentation/Node.cs
blob: e1f8b11bb1b7813a3e8606cb678545e40bdc7238 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;

namespace ARMeilleure.IntermediateRepresentation
{
    class Node : IIntrusiveListNode<Node>
    {
        public Node ListPrevious { get; set; }
        public Node ListNext { get; set; }

        public Operand Destination
        {
            get
            {
                return _destinations.Length != 0 ? GetDestination(0) : null;
            }
            set
            {
                if (value != null)
                {
                    SetDestinations(new Operand[] { value });
                }
                else
                {
                    SetDestinations(new Operand[0]);
                }
            }
        }

        private Operand[] _destinations;
        private Operand[] _sources;

        public int DestinationsCount => _destinations.Length;
        public int SourcesCount      => _sources.Length;

        public Node(Operand destination, int sourcesCount)
        {
            Destination = destination;

            _sources = new Operand[sourcesCount];
        }

        public Node(Operand[] destinations, int sourcesCount)
        {
            SetDestinations(destinations ?? throw new ArgumentNullException(nameof(destinations)));

            _sources = new Operand[sourcesCount];
        }

        public Operand GetDestination(int index)
        {
            return _destinations[index];
        }

        public Operand GetSource(int index)
        {
            return _sources[index];
        }

        public void SetDestination(int index, Operand destination)
        {
            Operand oldOp = _destinations[index];

            if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
            {
                oldOp.Assignments.Remove(this);
            }

            if (destination != null && destination.Kind == OperandKind.LocalVariable)
            {
                destination.Assignments.Add(this);
            }

            _destinations[index] = destination;
        }

        public void SetSource(int index, Operand source)
        {
            Operand oldOp = _sources[index];

            if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
            {
                oldOp.Uses.Remove(this);
            }

            if (source != null && source.Kind == OperandKind.LocalVariable)
            {
                source.Uses.Add(this);
            }

            _sources[index] = source;
        }

        public void SetDestinations(Operand[] destinations)
        {
            if (_destinations != null)
            {
                for (int index = 0; index < _destinations.Length; index++)
                {
                    Operand oldOp = _destinations[index];

                    if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
                    {
                        oldOp.Assignments.Remove(this);
                    }
                }

                _destinations = destinations;
            }
            else
            {
                _destinations = new Operand[destinations.Length];
            }

            for (int index = 0; index < destinations.Length; index++)
            {
                Operand newOp = destinations[index];

                _destinations[index] = newOp;

                if (newOp.Kind == OperandKind.LocalVariable)
                {
                    newOp.Assignments.Add(this);
                }
            }
        }

        public void SetSources(Operand[] sources)
        {
            for (int index = 0; index < _sources.Length; index++)
            {
                Operand oldOp = _sources[index];

                if (oldOp != null && oldOp.Kind == OperandKind.LocalVariable)
                {
                    oldOp.Uses.Remove(this);
                }
            }

            _sources = new Operand[sources.Length];

            for (int index = 0; index < sources.Length; index++)
            {
                Operand newOp = sources[index];

                _sources[index] = newOp;

                if (newOp.Kind == OperandKind.LocalVariable)
                {
                    newOp.Uses.Add(this);
                }
            }
        }
    }
}