AILOpCodeStore.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using ChocolArm64.State;
  2. using System.Reflection.Emit;
  3. namespace ChocolArm64.Translation
  4. {
  5. struct AILOpCodeStore : IAILEmit
  6. {
  7. public int Index { get; private set; }
  8. public AIoType IoType { get; private set; }
  9. public ARegisterSize RegisterSize { get; private set; }
  10. public AILOpCodeStore(int Index, AIoType IoType, ARegisterSize RegisterSize = 0)
  11. {
  12. this.Index = Index;
  13. this.IoType = IoType;
  14. this.RegisterSize = RegisterSize;
  15. }
  16. public void Emit(AILEmitter Context)
  17. {
  18. switch (IoType)
  19. {
  20. case AIoType.Arg: Context.Generator.EmitStarg(Index); break;
  21. case AIoType.Fields:
  22. {
  23. long IntOutputs = Context.LocalAlloc.GetIntOutputs(Context.GetILBlock(Index));
  24. long VecOutputs = Context.LocalAlloc.GetVecOutputs(Context.GetILBlock(Index));
  25. StoreLocals(Context, IntOutputs, ARegisterType.Int);
  26. StoreLocals(Context, VecOutputs, ARegisterType.Vector);
  27. break;
  28. }
  29. case AIoType.Flag: EmitStloc(Context, Index, ARegisterType.Flag); break;
  30. case AIoType.Int: EmitStloc(Context, Index, ARegisterType.Int); break;
  31. case AIoType.Vector: EmitStloc(Context, Index, ARegisterType.Vector); break;
  32. }
  33. }
  34. private void StoreLocals(AILEmitter Context, long Outputs, ARegisterType BaseType)
  35. {
  36. for (int Bit = 0; Bit < 64; Bit++)
  37. {
  38. long Mask = 1L << Bit;
  39. if ((Outputs & Mask) != 0)
  40. {
  41. ARegister Reg = AILEmitter.GetRegFromBit(Bit, BaseType);
  42. Context.Generator.EmitLdarg(ATranslatedSub.StateArgIdx);
  43. Context.Generator.EmitLdloc(Context.GetLocalIndex(Reg));
  44. Context.Generator.Emit(OpCodes.Stfld, Reg.GetField());
  45. }
  46. }
  47. }
  48. private void EmitStloc(AILEmitter Context, int Index, ARegisterType RegisterType)
  49. {
  50. ARegister Reg = new ARegister(Index, RegisterType);
  51. if (RegisterType == ARegisterType.Int &&
  52. RegisterSize == ARegisterSize.Int32)
  53. {
  54. Context.Generator.Emit(OpCodes.Conv_U8);
  55. }
  56. Context.Generator.EmitStloc(Context.GetLocalIndex(Reg));
  57. }
  58. }
  59. }