InstEmitFlow.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.Instructions
  6. {
  7. static partial class InstEmit
  8. {
  9. public static void B(ILEmitterCtx context)
  10. {
  11. OpCodeBImmAl64 op = (OpCodeBImmAl64)context.CurrOp;
  12. if (context.CurrBlock.Branch != null)
  13. {
  14. context.Emit(OpCodes.Br, context.GetLabel(op.Imm));
  15. }
  16. else
  17. {
  18. context.EmitStoreState();
  19. context.EmitLdc_I8(op.Imm);
  20. context.Emit(OpCodes.Ret);
  21. }
  22. }
  23. public static void B_Cond(ILEmitterCtx context)
  24. {
  25. OpCodeBImmCond64 op = (OpCodeBImmCond64)context.CurrOp;
  26. EmitBranch(context, op.Cond);
  27. }
  28. public static void Bl(ILEmitterCtx context)
  29. {
  30. OpCodeBImmAl64 op = (OpCodeBImmAl64)context.CurrOp;
  31. context.EmitLdc_I(op.Position + 4);
  32. context.EmitStint(CpuThreadState.LrIndex);
  33. context.EmitStoreState();
  34. if (context.TryOptEmitSubroutineCall())
  35. {
  36. //Note: the return value of the called method will be placed
  37. //at the Stack, the return value is always a Int64 with the
  38. //return address of the function. We check if the address is
  39. //correct, if it isn't we keep returning until we reach the dispatcher.
  40. context.Emit(OpCodes.Dup);
  41. context.EmitLdc_I8(op.Position + 4);
  42. ILLabel lblContinue = new ILLabel();
  43. context.Emit(OpCodes.Beq_S, lblContinue);
  44. context.Emit(OpCodes.Ret);
  45. context.MarkLabel(lblContinue);
  46. context.Emit(OpCodes.Pop);
  47. context.EmitLoadState();
  48. }
  49. else
  50. {
  51. context.EmitLdc_I8(op.Imm);
  52. context.Emit(OpCodes.Ret);
  53. }
  54. }
  55. public static void Blr(ILEmitterCtx context)
  56. {
  57. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  58. context.EmitLdintzr(op.Rn);
  59. context.EmitLdc_I(op.Position + 4);
  60. context.EmitStint(CpuThreadState.LrIndex);
  61. context.EmitStoreState();
  62. context.Emit(OpCodes.Ret);
  63. }
  64. public static void Br(ILEmitterCtx context)
  65. {
  66. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  67. context.EmitStoreState();
  68. context.EmitLdintzr(op.Rn);
  69. context.Emit(OpCodes.Ret);
  70. }
  71. public static void Cbnz(ILEmitterCtx context) => EmitCb(context, OpCodes.Bne_Un);
  72. public static void Cbz(ILEmitterCtx context) => EmitCb(context, OpCodes.Beq);
  73. private static void EmitCb(ILEmitterCtx context, OpCode ilOp)
  74. {
  75. OpCodeBImmCmp64 op = (OpCodeBImmCmp64)context.CurrOp;
  76. context.EmitLdintzr(op.Rt);
  77. context.EmitLdc_I(0);
  78. EmitBranch(context, ilOp);
  79. }
  80. public static void Ret(ILEmitterCtx context)
  81. {
  82. context.EmitStoreState();
  83. context.EmitLdint(CpuThreadState.LrIndex);
  84. context.Emit(OpCodes.Ret);
  85. }
  86. public static void Tbnz(ILEmitterCtx context) => EmitTb(context, OpCodes.Bne_Un);
  87. public static void Tbz(ILEmitterCtx context) => EmitTb(context, OpCodes.Beq);
  88. private static void EmitTb(ILEmitterCtx context, OpCode ilOp)
  89. {
  90. OpCodeBImmTest64 op = (OpCodeBImmTest64)context.CurrOp;
  91. context.EmitLdintzr(op.Rt);
  92. context.EmitLdc_I(1L << op.Pos);
  93. context.Emit(OpCodes.And);
  94. context.EmitLdc_I(0);
  95. EmitBranch(context, ilOp);
  96. }
  97. private static void EmitBranch(ILEmitterCtx context, Cond cond)
  98. {
  99. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  100. if (context.CurrBlock.Next != null &&
  101. context.CurrBlock.Branch != null)
  102. {
  103. context.EmitCondBranch(context.GetLabel(op.Imm), cond);
  104. }
  105. else
  106. {
  107. context.EmitStoreState();
  108. ILLabel lblTaken = new ILLabel();
  109. context.EmitCondBranch(lblTaken, cond);
  110. context.EmitLdc_I8(op.Position + 4);
  111. context.Emit(OpCodes.Ret);
  112. context.MarkLabel(lblTaken);
  113. context.EmitLdc_I8(op.Imm);
  114. context.Emit(OpCodes.Ret);
  115. }
  116. }
  117. private static void EmitBranch(ILEmitterCtx context, OpCode ilOp)
  118. {
  119. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  120. if (context.CurrBlock.Next != null &&
  121. context.CurrBlock.Branch != null)
  122. {
  123. context.Emit(ilOp, context.GetLabel(op.Imm));
  124. }
  125. else
  126. {
  127. context.EmitStoreState();
  128. ILLabel lblTaken = new ILLabel();
  129. context.Emit(ilOp, lblTaken);
  130. context.EmitLdc_I8(op.Position + 4);
  131. context.Emit(OpCodes.Ret);
  132. context.MarkLabel(lblTaken);
  133. context.EmitLdc_I8(op.Imm);
  134. context.Emit(OpCodes.Ret);
  135. }
  136. }
  137. }
  138. }