ILOpCodeStore.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using ChocolArm64.State;
  2. using System.Reflection.Emit;
  3. namespace ChocolArm64.Translation
  4. {
  5. struct IlOpCodeStore : 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 IlOpCodeStore(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.EmitStarg(Index); break;
  21. case IoType.Fields:
  22. {
  23. long intOutputs = context.LocalAlloc.GetIntOutputs(context.GetIlBlock(Index));
  24. long vecOutputs = context.LocalAlloc.GetVecOutputs(context.GetIlBlock(Index));
  25. StoreLocals(context, intOutputs, RegisterType.Int);
  26. StoreLocals(context, vecOutputs, RegisterType.Vector);
  27. break;
  28. }
  29. case IoType.Flag: EmitStloc(context, Index, RegisterType.Flag); break;
  30. case IoType.Int: EmitStloc(context, Index, RegisterType.Int); break;
  31. case IoType.Vector: EmitStloc(context, Index, RegisterType.Vector); break;
  32. }
  33. }
  34. private void StoreLocals(ILEmitter context, long outputs, RegisterType baseType)
  35. {
  36. for (int bit = 0; bit < 64; bit++)
  37. {
  38. long mask = 1L << bit;
  39. if ((outputs & mask) != 0)
  40. {
  41. Register reg = ILEmitter.GetRegFromBit(bit, baseType);
  42. context.Generator.EmitLdarg(TranslatedSub.StateArgIdx);
  43. context.Generator.EmitLdloc(context.GetLocalIndex(reg));
  44. context.Generator.Emit(OpCodes.Stfld, reg.GetField());
  45. }
  46. }
  47. }
  48. private void EmitStloc(ILEmitter context, int index, RegisterType registerType)
  49. {
  50. Register reg = new Register(index, registerType);
  51. if (registerType == RegisterType.Int &&
  52. RegisterSize == RegisterSize.Int32)
  53. {
  54. context.Generator.Emit(OpCodes.Conv_U8);
  55. }
  56. context.Generator.EmitStloc(context.GetLocalIndex(reg));
  57. }
  58. }
  59. }