InstEmitFlow.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(context.CurrBlock.Next);
  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.EmitSttmp();
  60. context.EmitLdc_I(op.Position + 4);
  61. context.EmitStint(CpuThreadState.LrIndex);
  62. context.EmitStoreState();
  63. context.EmitLdtmp();
  64. context.Emit(OpCodes.Ret);
  65. }
  66. public static void Br(ILEmitterCtx context)
  67. {
  68. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  69. context.EmitStoreState();
  70. context.EmitLdintzr(op.Rn);
  71. context.Emit(OpCodes.Ret);
  72. }
  73. public static void Cbnz(ILEmitterCtx context) => EmitCb(context, OpCodes.Bne_Un);
  74. public static void Cbz(ILEmitterCtx context) => EmitCb(context, OpCodes.Beq);
  75. private static void EmitCb(ILEmitterCtx context, OpCode ilOp)
  76. {
  77. OpCodeBImmCmp64 op = (OpCodeBImmCmp64)context.CurrOp;
  78. context.EmitLdintzr(op.Rt);
  79. context.EmitLdc_I(0);
  80. EmitBranch(context, ilOp);
  81. }
  82. public static void Ret(ILEmitterCtx context)
  83. {
  84. context.EmitStoreState();
  85. context.EmitLdint(CpuThreadState.LrIndex);
  86. context.Emit(OpCodes.Ret);
  87. }
  88. public static void Tbnz(ILEmitterCtx context) => EmitTb(context, OpCodes.Bne_Un);
  89. public static void Tbz(ILEmitterCtx context) => EmitTb(context, OpCodes.Beq);
  90. private static void EmitTb(ILEmitterCtx context, OpCode ilOp)
  91. {
  92. OpCodeBImmTest64 op = (OpCodeBImmTest64)context.CurrOp;
  93. context.EmitLdintzr(op.Rt);
  94. context.EmitLdc_I(1L << op.Pos);
  95. context.Emit(OpCodes.And);
  96. context.EmitLdc_I(0);
  97. EmitBranch(context, ilOp);
  98. }
  99. private static void EmitBranch(ILEmitterCtx context, Cond cond)
  100. {
  101. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  102. if (context.CurrBlock.Next != null &&
  103. context.CurrBlock.Branch != null)
  104. {
  105. context.EmitCondBranch(context.GetLabel(op.Imm), cond);
  106. }
  107. else
  108. {
  109. context.EmitStoreState();
  110. ILLabel lblTaken = new ILLabel();
  111. context.EmitCondBranch(lblTaken, cond);
  112. context.EmitLdc_I8(op.Position + 4);
  113. context.Emit(OpCodes.Ret);
  114. context.MarkLabel(lblTaken);
  115. context.EmitLdc_I8(op.Imm);
  116. context.Emit(OpCodes.Ret);
  117. }
  118. }
  119. private static void EmitBranch(ILEmitterCtx context, OpCode ilOp)
  120. {
  121. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  122. if (context.CurrBlock.Next != null &&
  123. context.CurrBlock.Branch != null)
  124. {
  125. context.Emit(ilOp, context.GetLabel(op.Imm));
  126. }
  127. else
  128. {
  129. context.EmitStoreState();
  130. ILLabel lblTaken = new ILLabel();
  131. context.Emit(ilOp, lblTaken);
  132. context.EmitLdc_I8(op.Position + 4);
  133. context.Emit(OpCodes.Ret);
  134. context.MarkLabel(lblTaken);
  135. context.EmitLdc_I8(op.Imm);
  136. context.Emit(OpCodes.Ret);
  137. }
  138. }
  139. }
  140. }