AInstEmitBfm.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using ChocolArm64.Translation;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.Instruction
  6. {
  7. static partial class AInstEmit
  8. {
  9. public static void Bfm(AILEmitterCtx Context)
  10. {
  11. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  12. EmitBfmLoadRn(Context);
  13. Context.EmitLdintzr(Op.Rd);
  14. Context.EmitLdc_I(~Op.WMask & Op.TMask);
  15. Context.Emit(OpCodes.And);
  16. Context.Emit(OpCodes.Or);
  17. Context.EmitLdintzr(Op.Rd);
  18. Context.EmitLdc_I(~Op.TMask);
  19. Context.Emit(OpCodes.And);
  20. Context.Emit(OpCodes.Or);
  21. Context.EmitStintzr(Op.Rd);
  22. }
  23. public static void Sbfm(AILEmitterCtx Context)
  24. {
  25. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  26. int BitsCount = Op.GetBitsCount();
  27. if (Op.Pos + 1 == BitsCount)
  28. {
  29. EmitSbfmShift(Context);
  30. }
  31. else if (Op.Pos < Op.Shift)
  32. {
  33. EmitSbfiz(Context);
  34. }
  35. else if (Op.Pos == 7 && Op.Shift == 0)
  36. {
  37. EmitSbfmCast(Context, OpCodes.Conv_I1);
  38. }
  39. else if (Op.Pos == 15 && Op.Shift == 0)
  40. {
  41. EmitSbfmCast(Context, OpCodes.Conv_I2);
  42. }
  43. else if (Op.Pos == 31 && Op.Shift == 0)
  44. {
  45. EmitSbfmCast(Context, OpCodes.Conv_I4);
  46. }
  47. else
  48. {
  49. EmitBfmLoadRn(Context);
  50. Context.EmitLdintzr(Op.Rn);
  51. Context.EmitLsl(BitsCount - 1 - Op.Pos);
  52. Context.EmitAsr(BitsCount - 1);
  53. Context.EmitLdc_I(~Op.TMask);
  54. Context.Emit(OpCodes.And);
  55. Context.Emit(OpCodes.Or);
  56. Context.EmitStintzr(Op.Rd);
  57. }
  58. }
  59. public static void Ubfm(AILEmitterCtx Context)
  60. {
  61. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  62. if (Op.Pos + 1 == Op.GetBitsCount())
  63. {
  64. EmitUbfmShift(Context);
  65. }
  66. else if (Op.Pos < Op.Shift)
  67. {
  68. EmitUbfiz(Context);
  69. }
  70. else if (Op.Pos + 1 == Op.Shift)
  71. {
  72. EmitBfmLsl(Context);
  73. }
  74. else if (Op.Pos == 7 && Op.Shift == 0)
  75. {
  76. EmitUbfmCast(Context, OpCodes.Conv_U1);
  77. }
  78. else if (Op.Pos == 15 && Op.Shift == 0)
  79. {
  80. EmitUbfmCast(Context, OpCodes.Conv_U2);
  81. }
  82. else
  83. {
  84. EmitBfmLoadRn(Context);
  85. Context.EmitStintzr(Op.Rd);
  86. }
  87. }
  88. private static void EmitSbfiz(AILEmitterCtx Context) => EmitBfiz(Context, true);
  89. private static void EmitUbfiz(AILEmitterCtx Context) => EmitBfiz(Context, false);
  90. private static void EmitBfiz(AILEmitterCtx Context, bool Signed)
  91. {
  92. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  93. int Width = Op.Pos + 1;
  94. Context.EmitLdintzr(Op.Rn);
  95. Context.EmitLsl(Op.GetBitsCount() - Width);
  96. if (Signed)
  97. {
  98. Context.EmitAsr(Op.Shift - Width);
  99. }
  100. else
  101. {
  102. Context.EmitLsr(Op.Shift - Width);
  103. }
  104. Context.EmitStintzr(Op.Rd);
  105. }
  106. private static void EmitSbfmCast(AILEmitterCtx Context, OpCode ILOp)
  107. {
  108. EmitBfmCast(Context, ILOp, true);
  109. }
  110. private static void EmitUbfmCast(AILEmitterCtx Context, OpCode ILOp)
  111. {
  112. EmitBfmCast(Context, ILOp, false);
  113. }
  114. private static void EmitBfmCast(AILEmitterCtx Context, OpCode ILOp, bool Signed)
  115. {
  116. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  117. Context.EmitLdintzr(Op.Rn);
  118. Context.Emit(ILOp);
  119. if (Op.RegisterSize != ARegisterSize.Int32)
  120. {
  121. Context.Emit(Signed
  122. ? OpCodes.Conv_I8
  123. : OpCodes.Conv_U8);
  124. }
  125. Context.EmitStintzr(Op.Rd);
  126. }
  127. private static void EmitSbfmShift(AILEmitterCtx Context)
  128. {
  129. EmitBfmShift(Context, true);
  130. }
  131. private static void EmitUbfmShift(AILEmitterCtx Context)
  132. {
  133. EmitBfmShift(Context, false);
  134. }
  135. private static void EmitBfmShift(AILEmitterCtx Context, bool Signed)
  136. {
  137. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  138. Context.EmitLdintzr(Op.Rn);
  139. Context.EmitLdc_I4(Op.Shift);
  140. Context.Emit(Signed
  141. ? OpCodes.Shr
  142. : OpCodes.Shr_Un);
  143. Context.EmitStintzr(Op.Rd);
  144. }
  145. private static void EmitBfmLsl(AILEmitterCtx Context)
  146. {
  147. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  148. Context.EmitLdintzr(Op.Rn);
  149. Context.EmitLsl(Op.GetBitsCount() - Op.Shift);
  150. Context.EmitStintzr(Op.Rd);
  151. }
  152. private static void EmitBfmLoadRn(AILEmitterCtx Context)
  153. {
  154. AOpCodeBfm Op = (AOpCodeBfm)Context.CurrOp;
  155. Context.EmitLdintzr(Op.Rn);
  156. Context.EmitRor(Op.Shift);
  157. Context.EmitLdc_I(Op.WMask & Op.TMask);
  158. Context.Emit(OpCodes.And);
  159. }
  160. }
  161. }