Operand.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace ARMeilleure.IntermediateRepresentation
  5. {
  6. class Operand
  7. {
  8. public OperandKind Kind { get; private set; }
  9. public OperandType Type { get; private set; }
  10. public ulong Value { get; private set; }
  11. public bool Relocatable { get; private set; }
  12. public int? PtcIndex { get; private set; }
  13. public List<Node> Assignments { get; }
  14. public List<Node> Uses { get; }
  15. public Operand()
  16. {
  17. Assignments = new List<Node>();
  18. Uses = new List<Node>();
  19. }
  20. public Operand(OperandKind kind, OperandType type = OperandType.None) : this()
  21. {
  22. Kind = kind;
  23. Type = type;
  24. }
  25. public Operand With(
  26. OperandKind kind,
  27. OperandType type = OperandType.None,
  28. ulong value = 0,
  29. bool relocatable = false,
  30. int? index = null)
  31. {
  32. Kind = kind;
  33. Type = type;
  34. Value = value;
  35. Relocatable = relocatable;
  36. PtcIndex = index;
  37. Assignments.Clear();
  38. Uses.Clear();
  39. return this;
  40. }
  41. public Operand With(int value)
  42. {
  43. return With(OperandKind.Constant, OperandType.I32, (uint)value);
  44. }
  45. public Operand With(uint value)
  46. {
  47. return With(OperandKind.Constant, OperandType.I32, value);
  48. }
  49. public Operand With(long value, bool relocatable = false, int? index = null)
  50. {
  51. return With(OperandKind.Constant, OperandType.I64, (ulong)value, relocatable, index);
  52. }
  53. public Operand With(ulong value)
  54. {
  55. return With(OperandKind.Constant, OperandType.I64, value);
  56. }
  57. public Operand With(float value)
  58. {
  59. return With(OperandKind.Constant, OperandType.FP32, (ulong)BitConverter.SingleToInt32Bits(value));
  60. }
  61. public Operand With(double value)
  62. {
  63. return With(OperandKind.Constant, OperandType.FP64, (ulong)BitConverter.DoubleToInt64Bits(value));
  64. }
  65. public Operand With(int index, RegisterType regType, OperandType type)
  66. {
  67. return With(OperandKind.Register, type, (ulong)((int)regType << 24 | index));
  68. }
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public Register GetRegister()
  71. {
  72. return new Register((int)Value & 0xffffff, (RegisterType)(Value >> 24));
  73. }
  74. public byte AsByte()
  75. {
  76. return (byte)Value;
  77. }
  78. public short AsInt16()
  79. {
  80. return (short)Value;
  81. }
  82. public int AsInt32()
  83. {
  84. return (int)Value;
  85. }
  86. public long AsInt64()
  87. {
  88. return (long)Value;
  89. }
  90. public float AsFloat()
  91. {
  92. return BitConverter.Int32BitsToSingle((int)Value);
  93. }
  94. public double AsDouble()
  95. {
  96. return BitConverter.Int64BitsToDouble((long)Value);
  97. }
  98. internal void NumberLocal(int number)
  99. {
  100. if (Kind != OperandKind.LocalVariable)
  101. {
  102. throw new InvalidOperationException("The operand is not a local variable.");
  103. }
  104. Value = (ulong)number;
  105. }
  106. public override int GetHashCode()
  107. {
  108. if (Kind == OperandKind.LocalVariable)
  109. {
  110. return base.GetHashCode();
  111. }
  112. else
  113. {
  114. return (int)Value ^ ((int)Kind << 16) ^ ((int)Type << 20);
  115. }
  116. }
  117. }
  118. }