Operand.cs 3.9 KB

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