Operand.cs 3.6 KB

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