Operation.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Reflection;
  4. using System.Reflection.Emit;
  5. namespace ChocolArm64.IntermediateRepresentation
  6. {
  7. class Operation
  8. {
  9. public BasicBlock Parent { get; set; }
  10. public OperationType Type { get; }
  11. private object[] _arguments { get; }
  12. private Operation(OperationType type, params object[] arguments)
  13. {
  14. Type = type;
  15. _arguments = arguments;
  16. }
  17. public T GetArg<T>(int index)
  18. {
  19. return (T)GetArg(index);
  20. }
  21. public object GetArg(int index)
  22. {
  23. if ((uint)index >= _arguments.Length)
  24. {
  25. throw new ArgumentOutOfRangeException(nameof(index));
  26. }
  27. return _arguments[index];
  28. }
  29. public static Operation Call(MethodInfo info)
  30. {
  31. return new Operation(OperationType.Call, info);
  32. }
  33. public static Operation CallVirtual(MethodInfo info)
  34. {
  35. return new Operation(OperationType.CallVirtual, info);
  36. }
  37. public static Operation IL(OpCode ilOp)
  38. {
  39. return new Operation(OperationType.IL, ilOp);
  40. }
  41. public static Operation ILBranch(OpCode ilOp, ILLabel target)
  42. {
  43. return new Operation(OperationType.ILBranch, ilOp, target);
  44. }
  45. public static Operation LoadArgument(int index)
  46. {
  47. return new Operation(OperationType.LoadArgument, index);
  48. }
  49. public static Operation LoadConstant(int value)
  50. {
  51. return new Operation(OperationType.LoadConstant, value);
  52. }
  53. public static Operation LoadConstant(long value)
  54. {
  55. return new Operation(OperationType.LoadConstant, value);
  56. }
  57. public static Operation LoadConstant(float value)
  58. {
  59. return new Operation(OperationType.LoadConstant, value);
  60. }
  61. public static Operation LoadConstant(double value)
  62. {
  63. return new Operation(OperationType.LoadConstant, value);
  64. }
  65. public static Operation LoadContext()
  66. {
  67. return new Operation(OperationType.LoadContext);
  68. }
  69. public static Operation LoadField(FieldInfo info)
  70. {
  71. return new Operation(OperationType.LoadField, info);
  72. }
  73. public static Operation LoadLocal(int index, RegisterType type, RegisterSize size)
  74. {
  75. return new Operation(OperationType.LoadLocal, index, type, size);
  76. }
  77. public static Operation MarkLabel(ILLabel label)
  78. {
  79. return new Operation(OperationType.MarkLabel, label);
  80. }
  81. public static Operation StoreContext()
  82. {
  83. return new Operation(OperationType.StoreContext);
  84. }
  85. public static Operation StoreLocal(int index, RegisterType type, RegisterSize size)
  86. {
  87. return new Operation(OperationType.StoreLocal, index, type, size);
  88. }
  89. }
  90. }