aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/IntermediateRepresentation/Operation.cs
blob: 955beafded3e51f0df0376e4a81f96f1f0bf3f68 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Diagnostics;

namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
{
    class Operation : INode
    {
        public Instruction Inst { get; private set; }

        private Operand[] _dests;

        public Operand Dest
        {
            get
            {
                return _dests.Length != 0 ? _dests[0] : null;
            }
            set
            {
                if (value != null && value.Type == OperandType.LocalVariable)
                {
                    value.AsgOp = this;
                }

                if (value != null)
                {
                    _dests = new[] { value };
                }
                else
                {
                    _dests = Array.Empty<Operand>();
                }
            }
        }

        public int DestsCount => _dests.Length;

        private Operand[] _sources;

        public int SourcesCount => _sources.Length;

        public int Index { get; }

        private Operation(Operand[] sources)
        {
            // The array may be modified externally, so we store a copy.
            _sources = (Operand[])sources.Clone();

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

                if (source.Type == OperandType.LocalVariable)
                {
                    source.UseOps.Add(this);
                }
            }
        }

        public Operation(Instruction inst, int index, Operand[] dests, Operand[] sources) : this(sources)
        {
            Inst  = inst;
            Index = index;

            // The array may be modified externally, so we store a copy.
            _dests = (Operand[])dests.Clone();

            for (int dstIndex = 0; dstIndex < dests.Length; dstIndex++)
            {
                Operand dest = dests[dstIndex];

                if (dest != null && dest.Type == OperandType.LocalVariable)
                {
                    dest.AsgOp = this;
                }
            }
        }

        public Operation(Instruction inst, Operand dest, params Operand[] sources) : this(sources)
        {
            Inst = inst;

            if (dest != null)
            {
                dest.AsgOp = this;

                _dests = new[] { dest };
            }
            else
            {
                _dests = Array.Empty<Operand>();
            }
        }

        public Operation(Instruction inst, int index, Operand dest, params Operand[] sources) : this(inst, dest, sources)
        {
            Index = index;
        }

        public void AppendDests(Operand[] operands)
        {
            int startIndex = _dests.Length;

            Array.Resize(ref _dests, startIndex + operands.Length);

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

                if (dest != null && dest.Type == OperandType.LocalVariable)
                {
                    Debug.Assert(dest.AsgOp == null);
                    dest.AsgOp = this;
                }

                _dests[startIndex + index] = dest;
            }
        }

        public void AppendSources(Operand[] operands)
        {
            int startIndex = _sources.Length;

            Array.Resize(ref _sources, startIndex + operands.Length);

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

                if (source.Type == OperandType.LocalVariable)
                {
                    source.UseOps.Add(this);
                }

                _sources[startIndex + index] = source;
            }
        }

        public Operand GetDest(int index)
        {
            return _dests[index];
        }

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

        public void SetDest(int index, Operand dest)
        {
            Operand oldDest = _dests[index];

            if (oldDest != null && oldDest.Type == OperandType.LocalVariable)
            {
                oldDest.AsgOp = null;
            }

            if (dest != null && dest.Type == OperandType.LocalVariable)
            {
                dest.AsgOp = this;
            }

            _dests[index] = dest;
        }

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

            if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
            {
                oldSrc.UseOps.Remove(this);
            }

            if (source != null && source.Type == OperandType.LocalVariable)
            {
                source.UseOps.Add(this);
            }

            _sources[index] = source;
        }

        protected void RemoveSource(int index)
        {
            SetSource(index, null);

            Operand[] newSources = new Operand[_sources.Length - 1];

            Array.Copy(_sources, 0, newSources, 0, index);
            Array.Copy(_sources, index + 1, newSources, index, _sources.Length - (index + 1));

            _sources = newSources;
        }

        public void TurnIntoCopy(Operand source)
        {
            TurnInto(Instruction.Copy, source);
        }

        public void TurnInto(Instruction newInst, Operand source)
        {
            Inst = newInst;

            foreach (Operand oldSrc in _sources)
            {
                if (oldSrc != null && oldSrc.Type == OperandType.LocalVariable)
                {
                    oldSrc.UseOps.Remove(this);
                }
            }

            if (source.Type == OperandType.LocalVariable)
            {
                source.UseOps.Add(this);
            }

            _sources = new Operand[] { source };
        }
    }
}