InstEmitBfm.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 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(ILEmitterCtx context)
  24. {
  25. OpCodeBfm64 op = (OpCodeBfm64)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(ILEmitterCtx context)
  60. {
  61. OpCodeBfm64 op = (OpCodeBfm64)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(ILEmitterCtx context) => EmitBfiz(context, true);
  89. private static void EmitUbfiz(ILEmitterCtx context) => EmitBfiz(context, false);
  90. private static void EmitBfiz(ILEmitterCtx context, bool signed)
  91. {
  92. OpCodeBfm64 op = (OpCodeBfm64)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(ILEmitterCtx context, OpCode ilOp)
  107. {
  108. EmitBfmCast(context, ilOp, true);
  109. }
  110. private static void EmitUbfmCast(ILEmitterCtx context, OpCode ilOp)
  111. {
  112. EmitBfmCast(context, ilOp, false);
  113. }
  114. private static void EmitBfmCast(ILEmitterCtx context, OpCode ilOp, bool signed)
  115. {
  116. OpCodeBfm64 op = (OpCodeBfm64)context.CurrOp;
  117. context.EmitLdintzr(op.Rn);
  118. context.Emit(ilOp);
  119. if (op.RegisterSize != RegisterSize.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(ILEmitterCtx context)
  128. {
  129. EmitBfmShift(context, true);
  130. }
  131. private static void EmitUbfmShift(ILEmitterCtx context)
  132. {
  133. EmitBfmShift(context, false);
  134. }
  135. private static void EmitBfmShift(ILEmitterCtx context, bool signed)
  136. {
  137. OpCodeBfm64 op = (OpCodeBfm64)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(ILEmitterCtx context)
  146. {
  147. OpCodeBfm64 op = (OpCodeBfm64)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(ILEmitterCtx context)
  153. {
  154. OpCodeBfm64 op = (OpCodeBfm64)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. }