InstEmitBfm.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Bfm(ILEmitterCtx context)
  10. {
  11. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  12. if (op.Pos < op.Shift)
  13. {
  14. // BFI.
  15. context.EmitLdintzr(op.Rn);
  16. int shift = op.GetBitsCount() - op.Shift;
  17. int width = op.Pos + 1;
  18. long mask = (long)(ulong.MaxValue >> (64 - width));
  19. context.EmitLdc_I(mask);
  20. context.Emit(OpCodes.And);
  21. context.EmitLsl(shift);
  22. context.EmitLdintzr(op.Rd);
  23. context.EmitLdc_I(~(mask << shift));
  24. context.Emit(OpCodes.And);
  25. context.Emit(OpCodes.Or);
  26. context.EmitStintzr(op.Rd);
  27. }
  28. else
  29. {
  30. // BFXIL.
  31. context.EmitLdintzr(op.Rn);
  32. context.EmitLsr(op.Shift);
  33. int width = op.Pos - op.Shift + 1;
  34. long mask = (long)(ulong.MaxValue >> (64 - width));
  35. context.EmitLdc_I(mask);
  36. context.Emit(OpCodes.And);
  37. context.EmitLdintzr(op.Rd);
  38. context.EmitLdc_I(~mask);
  39. context.Emit(OpCodes.And);
  40. context.Emit(OpCodes.Or);
  41. context.EmitStintzr(op.Rd);
  42. }
  43. }
  44. public static void Sbfm(ILEmitterCtx context)
  45. {
  46. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  47. int bitsCount = op.GetBitsCount();
  48. if (op.Pos + 1 == bitsCount)
  49. {
  50. EmitSbfmShift(context);
  51. }
  52. else if (op.Pos < op.Shift)
  53. {
  54. EmitSbfiz(context);
  55. }
  56. else if (op.Pos == 7 && op.Shift == 0)
  57. {
  58. EmitSbfmCast(context, OpCodes.Conv_I1);
  59. }
  60. else if (op.Pos == 15 && op.Shift == 0)
  61. {
  62. EmitSbfmCast(context, OpCodes.Conv_I2);
  63. }
  64. else if (op.Pos == 31 && op.Shift == 0)
  65. {
  66. EmitSbfmCast(context, OpCodes.Conv_I4);
  67. }
  68. else
  69. {
  70. EmitBfmLoadRn(context);
  71. context.EmitLdintzr(op.Rn);
  72. context.EmitLsl(bitsCount - 1 - op.Pos);
  73. context.EmitAsr(bitsCount - 1);
  74. context.EmitLdc_I(~op.TMask);
  75. context.Emit(OpCodes.And);
  76. context.Emit(OpCodes.Or);
  77. context.EmitStintzr(op.Rd);
  78. }
  79. }
  80. public static void Ubfm(ILEmitterCtx context)
  81. {
  82. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  83. if (op.Pos + 1 == op.GetBitsCount())
  84. {
  85. EmitUbfmShift(context);
  86. }
  87. else if (op.Pos < op.Shift)
  88. {
  89. EmitUbfiz(context);
  90. }
  91. else if (op.Pos + 1 == op.Shift)
  92. {
  93. EmitBfmLsl(context);
  94. }
  95. else if (op.Pos == 7 && op.Shift == 0)
  96. {
  97. EmitUbfmCast(context, OpCodes.Conv_U1);
  98. }
  99. else if (op.Pos == 15 && op.Shift == 0)
  100. {
  101. EmitUbfmCast(context, OpCodes.Conv_U2);
  102. }
  103. else
  104. {
  105. EmitBfmLoadRn(context);
  106. context.EmitStintzr(op.Rd);
  107. }
  108. }
  109. private static void EmitSbfiz(ILEmitterCtx context) => EmitBfiz(context, true);
  110. private static void EmitUbfiz(ILEmitterCtx context) => EmitBfiz(context, false);
  111. private static void EmitBfiz(ILEmitterCtx context, bool signed)
  112. {
  113. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  114. int width = op.Pos + 1;
  115. context.EmitLdintzr(op.Rn);
  116. context.EmitLsl(op.GetBitsCount() - width);
  117. if (signed)
  118. {
  119. context.EmitAsr(op.Shift - width);
  120. }
  121. else
  122. {
  123. context.EmitLsr(op.Shift - width);
  124. }
  125. context.EmitStintzr(op.Rd);
  126. }
  127. private static void EmitSbfmCast(ILEmitterCtx context, OpCode ilOp)
  128. {
  129. EmitBfmCast(context, ilOp, true);
  130. }
  131. private static void EmitUbfmCast(ILEmitterCtx context, OpCode ilOp)
  132. {
  133. EmitBfmCast(context, ilOp, false);
  134. }
  135. private static void EmitBfmCast(ILEmitterCtx context, OpCode ilOp, bool signed)
  136. {
  137. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  138. context.EmitLdintzr(op.Rn);
  139. context.Emit(ilOp);
  140. if (op.RegisterSize != RegisterSize.Int32)
  141. {
  142. context.Emit(signed
  143. ? OpCodes.Conv_I8
  144. : OpCodes.Conv_U8);
  145. }
  146. context.EmitStintzr(op.Rd);
  147. }
  148. private static void EmitSbfmShift(ILEmitterCtx context)
  149. {
  150. EmitBfmShift(context, true);
  151. }
  152. private static void EmitUbfmShift(ILEmitterCtx context)
  153. {
  154. EmitBfmShift(context, false);
  155. }
  156. private static void EmitBfmShift(ILEmitterCtx context, bool signed)
  157. {
  158. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  159. context.EmitLdintzr(op.Rn);
  160. context.EmitLdc_I4(op.Shift);
  161. context.Emit(signed
  162. ? OpCodes.Shr
  163. : OpCodes.Shr_Un);
  164. context.EmitStintzr(op.Rd);
  165. }
  166. private static void EmitBfmLsl(ILEmitterCtx context)
  167. {
  168. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  169. context.EmitLdintzr(op.Rn);
  170. context.EmitLsl(op.GetBitsCount() - op.Shift);
  171. context.EmitStintzr(op.Rd);
  172. }
  173. private static void EmitBfmLoadRn(ILEmitterCtx context)
  174. {
  175. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  176. context.EmitLdintzr(op.Rn);
  177. context.EmitRor(op.Shift);
  178. context.EmitLdc_I(op.WMask & op.TMask);
  179. context.Emit(OpCodes.And);
  180. }
  181. }
  182. }