InstEmitFlow.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Bra(EmitterContext context)
  13. {
  14. EmitBranch(context, context.CurrBlock.Branch.Address);
  15. }
  16. public static void Brk(EmitterContext context)
  17. {
  18. EmitBrkOrSync(context);
  19. }
  20. public static void Brx(EmitterContext context)
  21. {
  22. OpCodeBranchIndir op = (OpCodeBranchIndir)context.CurrOp;
  23. int offset = (int)op.Address + 8 + op.Offset;
  24. Operand address = context.IAdd(Register(op.Ra), Const(offset));
  25. // Sorting the target addresses in descending order improves the code,
  26. // since it will always check the most distant targets first, then the
  27. // near ones. This can be easily transformed into if/else statements.
  28. IOrderedEnumerable<Block> sortedTargets = op.PossibleTargets.OrderByDescending(x => x.Address);
  29. Block lastTarget = sortedTargets.LastOrDefault();
  30. foreach (Block possibleTarget in sortedTargets)
  31. {
  32. Operand label = context.GetLabel(possibleTarget.Address);
  33. if (possibleTarget != lastTarget)
  34. {
  35. context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)possibleTarget.Address)));
  36. }
  37. else
  38. {
  39. context.Branch(label);
  40. }
  41. }
  42. }
  43. public static void Exit(EmitterContext context)
  44. {
  45. OpCodeExit op = (OpCodeExit)context.CurrOp;
  46. // TODO: Figure out how this is supposed to work in the
  47. // presence of other condition codes.
  48. if (op.Condition == Condition.Always)
  49. {
  50. context.Return();
  51. }
  52. }
  53. public static void Kil(EmitterContext context)
  54. {
  55. context.Discard();
  56. }
  57. public static void Pbk(EmitterContext context)
  58. {
  59. EmitPbkOrSsy(context);
  60. }
  61. public static void Ssy(EmitterContext context)
  62. {
  63. EmitPbkOrSsy(context);
  64. }
  65. public static void Sync(EmitterContext context)
  66. {
  67. EmitBrkOrSync(context);
  68. }
  69. private static void EmitPbkOrSsy(EmitterContext context)
  70. {
  71. OpCodePush op = (OpCodePush)context.CurrOp;
  72. foreach (KeyValuePair<OpCodeBranchPop, Operand> kv in op.PopOps)
  73. {
  74. OpCodeBranchPop opSync = kv.Key;
  75. Operand local = kv.Value;
  76. int pushOpIndex = opSync.Targets[op];
  77. context.Copy(local, Const(pushOpIndex));
  78. }
  79. }
  80. private static void EmitBrkOrSync(EmitterContext context)
  81. {
  82. OpCodeBranchPop op = (OpCodeBranchPop)context.CurrOp;
  83. if (op.Targets.Count == 1)
  84. {
  85. // If we have only one target, then the SSY/PBK is basically
  86. // a branch, we can produce better codegen for this case.
  87. OpCodePush pushOp = op.Targets.Keys.First();
  88. EmitBranch(context, pushOp.GetAbsoluteAddress());
  89. }
  90. else
  91. {
  92. foreach (KeyValuePair<OpCodePush, int> kv in op.Targets)
  93. {
  94. OpCodePush pushOp = kv.Key;
  95. Operand label = context.GetLabel(pushOp.GetAbsoluteAddress());
  96. Operand local = pushOp.PopOps[op];
  97. int pushOpIndex = kv.Value;
  98. context.BranchIfTrue(label, context.ICompareEqual(local, Const(pushOpIndex)));
  99. }
  100. }
  101. }
  102. private static void EmitBranch(EmitterContext context, ulong address)
  103. {
  104. OpCode op = context.CurrOp;
  105. // If we're branching to the next instruction, then the branch
  106. // is useless and we can ignore it.
  107. if (address == op.Address + 8)
  108. {
  109. return;
  110. }
  111. Operand label = context.GetLabel(address);
  112. Operand pred = Register(op.Predicate);
  113. if (op is OpCodeBranch opBranch && opBranch.Condition != Condition.Always)
  114. {
  115. pred = context.BitwiseAnd(pred, GetCondition(context, opBranch.Condition));
  116. }
  117. if (op.Predicate.IsPT)
  118. {
  119. context.Branch(label);
  120. }
  121. else if (op.InvertPredicate)
  122. {
  123. context.BranchIfFalse(label, pred);
  124. }
  125. else
  126. {
  127. context.BranchIfTrue(label, pred);
  128. }
  129. }
  130. private static Operand GetCondition(EmitterContext context, Condition cond)
  131. {
  132. // TODO: More condition codes, figure out how they work.
  133. switch (cond)
  134. {
  135. case Condition.Equal:
  136. case Condition.EqualUnordered:
  137. return GetZF();
  138. case Condition.NotEqual:
  139. case Condition.NotEqualUnordered:
  140. return context.BitwiseNot(GetZF());
  141. }
  142. return Const(IrConsts.True);
  143. }
  144. }
  145. }