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
|
using Ryujinx.Graphics.Shader.Decoders;
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using Ryujinx.Graphics.Shader.Translation;
using System.Collections.Generic;
using System.Linq;
using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Instructions
{
static partial class InstEmit
{
public static void Bra(EmitterContext context)
{
EmitBranch(context, context.CurrBlock.Branch.Address);
}
public static void Brk(EmitterContext context)
{
EmitBrkOrSync(context);
}
public static void Brx(EmitterContext context)
{
OpCodeBranchIndir op = (OpCodeBranchIndir)context.CurrOp;
int offset = (int)op.Address + 8 + op.Offset;
Operand address = context.IAdd(Register(op.Ra), Const(offset));
// Sorting the target addresses in descending order improves the code,
// since it will always check the most distant targets first, then the
// near ones. This can be easily transformed into if/else statements.
IOrderedEnumerable<Block> sortedTargets = op.PossibleTargets.OrderByDescending(x => x.Address);
Block lastTarget = sortedTargets.LastOrDefault();
foreach (Block possibleTarget in sortedTargets)
{
Operand label = context.GetLabel(possibleTarget.Address);
if (possibleTarget != lastTarget)
{
context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)possibleTarget.Address)));
}
else
{
context.Branch(label);
}
}
}
public static void Exit(EmitterContext context)
{
OpCodeExit op = (OpCodeExit)context.CurrOp;
// TODO: Figure out how this is supposed to work in the
// presence of other condition codes.
if (op.Condition == Condition.Always)
{
context.Return();
}
}
public static void Kil(EmitterContext context)
{
context.Discard();
}
public static void Pbk(EmitterContext context)
{
EmitPbkOrSsy(context);
}
public static void Ssy(EmitterContext context)
{
EmitPbkOrSsy(context);
}
public static void Sync(EmitterContext context)
{
EmitBrkOrSync(context);
}
private static void EmitPbkOrSsy(EmitterContext context)
{
OpCodePush op = (OpCodePush)context.CurrOp;
foreach (KeyValuePair<OpCodeBranchPop, Operand> kv in op.PopOps)
{
OpCodeBranchPop opSync = kv.Key;
Operand local = kv.Value;
int pushOpIndex = opSync.Targets[op];
context.Copy(local, Const(pushOpIndex));
}
}
private static void EmitBrkOrSync(EmitterContext context)
{
OpCodeBranchPop op = (OpCodeBranchPop)context.CurrOp;
if (op.Targets.Count == 1)
{
// If we have only one target, then the SSY/PBK is basically
// a branch, we can produce better codegen for this case.
OpCodePush pushOp = op.Targets.Keys.First();
EmitBranch(context, pushOp.GetAbsoluteAddress());
}
else
{
foreach (KeyValuePair<OpCodePush, int> kv in op.Targets)
{
OpCodePush pushOp = kv.Key;
Operand label = context.GetLabel(pushOp.GetAbsoluteAddress());
Operand local = pushOp.PopOps[op];
int pushOpIndex = kv.Value;
context.BranchIfTrue(label, context.ICompareEqual(local, Const(pushOpIndex)));
}
}
}
private static void EmitBranch(EmitterContext context, ulong address)
{
OpCode op = context.CurrOp;
// If we're branching to the next instruction, then the branch
// is useless and we can ignore it.
if (address == op.Address + 8)
{
return;
}
Operand label = context.GetLabel(address);
Operand pred = Register(op.Predicate);
if (op is OpCodeBranch opBranch && opBranch.Condition != Condition.Always)
{
pred = context.BitwiseAnd(pred, GetCondition(context, opBranch.Condition));
}
if (op.Predicate.IsPT)
{
context.Branch(label);
}
else if (op.InvertPredicate)
{
context.BranchIfFalse(label, pred);
}
else
{
context.BranchIfTrue(label, pred);
}
}
private static Operand GetCondition(EmitterContext context, Condition cond)
{
// TODO: More condition codes, figure out how they work.
switch (cond)
{
case Condition.Equal:
case Condition.EqualUnordered:
return GetZF();
case Condition.NotEqual:
case Condition.NotEqualUnordered:
return context.BitwiseNot(GetZF());
}
return Const(IrConsts.True);
}
}
}
|