Operand.cs 3.2 KB

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