InstEmitFlow.cs 7.0 KB

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