InstEmitFlow.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 Cal(EmitterContext context)
  44. {
  45. OpCodeBranch op = (OpCodeBranch)context.CurrOp;
  46. context.Call(context.GetFunctionId(op.GetAbsoluteAddress()), false);
  47. }
  48. public static void Depbar(EmitterContext context)
  49. {
  50. }
  51. public static void Exit(EmitterContext context)
  52. {
  53. if (context.IsNonMain)
  54. {
  55. context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
  56. return;
  57. }
  58. OpCodeExit op = (OpCodeExit)context.CurrOp;
  59. // TODO: Figure out how this is supposed to work in the
  60. // presence of other condition codes.
  61. if (op.Condition == Condition.Always)
  62. {
  63. context.Return();
  64. }
  65. }
  66. public static void Kil(EmitterContext context)
  67. {
  68. context.Discard();
  69. }
  70. public static void Nop(EmitterContext context)
  71. {
  72. }
  73. public static void Pbk(EmitterContext context)
  74. {
  75. EmitPbkOrSsy(context);
  76. }
  77. public static void Ret(EmitterContext context)
  78. {
  79. if (context.IsNonMain)
  80. {
  81. context.Return();
  82. }
  83. else
  84. {
  85. context.Config.GpuAccessor.Log("Invalid return on main function.");
  86. }
  87. }
  88. public static void Ssy(EmitterContext context)
  89. {
  90. EmitPbkOrSsy(context);
  91. }
  92. public static void Sync(EmitterContext context)
  93. {
  94. EmitBrkOrSync(context);
  95. }
  96. private static void EmitPbkOrSsy(EmitterContext context)
  97. {
  98. OpCodePush op = (OpCodePush)context.CurrOp;
  99. foreach (KeyValuePair<OpCodeBranchPop, Operand> kv in op.PopOps)
  100. {
  101. OpCodeBranchPop opSync = kv.Key;
  102. Operand local = kv.Value;
  103. int pushOpIndex = opSync.Targets[op];
  104. context.Copy(local, Const(pushOpIndex));
  105. }
  106. }
  107. private static void EmitBrkOrSync(EmitterContext context)
  108. {
  109. OpCodeBranchPop op = (OpCodeBranchPop)context.CurrOp;
  110. if (op.Targets.Count == 1)
  111. {
  112. // If we have only one target, then the SSY/PBK is basically
  113. // a branch, we can produce better codegen for this case.
  114. OpCodePush pushOp = op.Targets.Keys.First();
  115. EmitBranch(context, pushOp.GetAbsoluteAddress());
  116. }
  117. else
  118. {
  119. // TODO: Support CC here aswell (condition).
  120. foreach (KeyValuePair<OpCodePush, int> kv in op.Targets)
  121. {
  122. OpCodePush pushOp = kv.Key;
  123. Operand label = context.GetLabel(pushOp.GetAbsoluteAddress());
  124. Operand local = pushOp.PopOps[op];
  125. int pushOpIndex = kv.Value;
  126. context.BranchIfTrue(label, context.ICompareEqual(local, Const(pushOpIndex)));
  127. }
  128. }
  129. }
  130. private static void EmitBranch(EmitterContext context, ulong address)
  131. {
  132. OpCode op = context.CurrOp;
  133. // If we're branching to the next instruction, then the branch
  134. // is useless and we can ignore it.
  135. if (address == op.Address + 8)
  136. {
  137. return;
  138. }
  139. Operand label = context.GetLabel(address);
  140. Operand pred = Register(op.Predicate);
  141. if (op is OpCodeConditional opCond && opCond.Condition != Condition.Always)
  142. {
  143. Operand cond = GetCondition(context, opCond.Condition);
  144. if (op.Predicate.IsPT)
  145. {
  146. pred = cond;
  147. }
  148. else if (op.InvertPredicate)
  149. {
  150. pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
  151. }
  152. else
  153. {
  154. pred = context.BitwiseAnd(pred, cond);
  155. }
  156. context.BranchIfTrue(label, pred);
  157. }
  158. else if (op.Predicate.IsPT)
  159. {
  160. context.Branch(label);
  161. }
  162. else if (op.InvertPredicate)
  163. {
  164. context.BranchIfFalse(label, pred);
  165. }
  166. else
  167. {
  168. context.BranchIfTrue(label, pred);
  169. }
  170. }
  171. private static Operand GetCondition(EmitterContext context, Condition cond)
  172. {
  173. // TODO: More condition codes, figure out how they work.
  174. switch (cond)
  175. {
  176. case Condition.Equal:
  177. case Condition.EqualUnordered:
  178. return GetZF();
  179. case Condition.NotEqual:
  180. case Condition.NotEqualUnordered:
  181. return context.BitwiseNot(GetZF());
  182. }
  183. return Const(IrConsts.True);
  184. }
  185. }
  186. }