Operand.cs 3.0 KB

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