Operand.cs 3.9 KB

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