InstEmitFlow.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(RegisterAlias.Lr);
  33. context.EmitStoreState();
  34. InstEmitFlowHelper.EmitCall(context, op.Imm);
  35. }
  36. public static void Blr(ILEmitterCtx context)
  37. {
  38. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  39. context.EmitLdintzr(op.Rn);
  40. context.EmitLdc_I(op.Position + 4);
  41. context.EmitStint(RegisterAlias.Lr);
  42. context.EmitStoreState();
  43. context.Emit(OpCodes.Ret);
  44. }
  45. public static void Br(ILEmitterCtx context)
  46. {
  47. OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
  48. context.EmitStoreState();
  49. context.EmitLdintzr(op.Rn);
  50. context.Emit(OpCodes.Ret);
  51. }
  52. public static void Cbnz(ILEmitterCtx context) => EmitCb(context, OpCodes.Bne_Un);
  53. public static void Cbz(ILEmitterCtx context) => EmitCb(context, OpCodes.Beq);
  54. private static void EmitCb(ILEmitterCtx context, OpCode ilOp)
  55. {
  56. OpCodeBImmCmp64 op = (OpCodeBImmCmp64)context.CurrOp;
  57. context.EmitLdintzr(op.Rt);
  58. context.EmitLdc_I(0);
  59. EmitBranch(context, ilOp);
  60. }
  61. public static void Ret(ILEmitterCtx context)
  62. {
  63. context.EmitStoreState();
  64. context.EmitLdint(RegisterAlias.Lr);
  65. context.Emit(OpCodes.Ret);
  66. }
  67. public static void Tbnz(ILEmitterCtx context) => EmitTb(context, OpCodes.Bne_Un);
  68. public static void Tbz(ILEmitterCtx context) => EmitTb(context, OpCodes.Beq);
  69. private static void EmitTb(ILEmitterCtx context, OpCode ilOp)
  70. {
  71. OpCodeBImmTest64 op = (OpCodeBImmTest64)context.CurrOp;
  72. context.EmitLdintzr(op.Rt);
  73. context.EmitLdc_I(1L << op.Pos);
  74. context.Emit(OpCodes.And);
  75. context.EmitLdc_I(0);
  76. EmitBranch(context, ilOp);
  77. }
  78. private static void EmitBranch(ILEmitterCtx context, Condition cond)
  79. {
  80. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  81. if (context.CurrBlock.Next != null &&
  82. context.CurrBlock.Branch != null)
  83. {
  84. context.EmitCondBranch(context.GetLabel(op.Imm), cond);
  85. }
  86. else
  87. {
  88. context.EmitStoreState();
  89. ILLabel lblTaken = new ILLabel();
  90. context.EmitCondBranch(lblTaken, cond);
  91. context.EmitLdc_I8(op.Position + 4);
  92. context.Emit(OpCodes.Ret);
  93. context.MarkLabel(lblTaken);
  94. context.EmitLdc_I8(op.Imm);
  95. context.Emit(OpCodes.Ret);
  96. }
  97. }
  98. private static void EmitBranch(ILEmitterCtx context, OpCode ilOp)
  99. {
  100. OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
  101. if (context.CurrBlock.Next != null &&
  102. context.CurrBlock.Branch != null)
  103. {
  104. context.Emit(ilOp, context.GetLabel(op.Imm));
  105. }
  106. else
  107. {
  108. context.EmitStoreState();
  109. ILLabel lblTaken = new ILLabel();
  110. context.Emit(ilOp, lblTaken);
  111. context.EmitLdc_I8(op.Position + 4);
  112. context.Emit(OpCodes.Ret);
  113. context.MarkLabel(lblTaken);
  114. context.EmitLdc_I8(op.Imm);
  115. context.Emit(OpCodes.Ret);
  116. }
  117. }
  118. }
  119. }