ILOpCodeLoad.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using ChocolArm64.State;
  2. using System.Reflection.Emit;
  3. namespace ChocolArm64.Translation
  4. {
  5. struct IlOpCodeLoad : IILEmit
  6. {
  7. public int Index { get; private set; }
  8. public IoType IoType { get; private set; }
  9. public RegisterSize RegisterSize { get; private set; }
  10. public IlOpCodeLoad(int index, IoType ioType, RegisterSize registerSize = 0)
  11. {
  12. Index = index;
  13. IoType = ioType;
  14. RegisterSize = registerSize;
  15. }
  16. public void Emit(ILEmitter context)
  17. {
  18. switch (IoType)
  19. {
  20. case IoType.Arg: context.Generator.EmitLdarg(Index); break;
  21. case IoType.Fields:
  22. {
  23. long intInputs = context.LocalAlloc.GetIntInputs(context.GetIlBlock(Index));
  24. long vecInputs = context.LocalAlloc.GetVecInputs(context.GetIlBlock(Index));
  25. LoadLocals(context, intInputs, RegisterType.Int);
  26. LoadLocals(context, vecInputs, RegisterType.Vector);
  27. break;
  28. }
  29. case IoType.Flag: EmitLdloc(context, Index, RegisterType.Flag); break;
  30. case IoType.Int: EmitLdloc(context, Index, RegisterType.Int); break;
  31. case IoType.Vector: EmitLdloc(context, Index, RegisterType.Vector); break;
  32. }
  33. }
  34. private void LoadLocals(ILEmitter context, long inputs, RegisterType baseType)
  35. {
  36. for (int bit = 0; bit < 64; bit++)
  37. {
  38. long mask = 1L << bit;
  39. if ((inputs & mask) != 0)
  40. {
  41. Register reg = ILEmitter.GetRegFromBit(bit, baseType);
  42. context.Generator.EmitLdarg(TranslatedSub.StateArgIdx);
  43. context.Generator.Emit(OpCodes.Ldfld, reg.GetField());
  44. context.Generator.EmitStloc(context.GetLocalIndex(reg));
  45. }
  46. }
  47. }
  48. private void EmitLdloc(ILEmitter context, int index, RegisterType registerType)
  49. {
  50. Register reg = new Register(index, registerType);
  51. context.Generator.EmitLdloc(context.GetLocalIndex(reg));
  52. if (registerType == RegisterType.Int &&
  53. RegisterSize == RegisterSize.Int32)
  54. {
  55. context.Generator.Emit(OpCodes.Conv_U4);
  56. }
  57. }
  58. }
  59. }