InstEmitFlow.cs 4.7 KB

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