LiteralInteger.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.IO;
  3. namespace Spv.Generator
  4. {
  5. public class LiteralInteger : Operand, IEquatable<LiteralInteger>
  6. {
  7. [ThreadStatic]
  8. private static GeneratorPool<LiteralInteger> _pool;
  9. internal static void RegisterPool(GeneratorPool<LiteralInteger> pool)
  10. {
  11. _pool = pool;
  12. }
  13. internal static void UnregisterPool()
  14. {
  15. _pool = null;
  16. }
  17. public OperandType Type => OperandType.Number;
  18. private enum IntegerType
  19. {
  20. UInt32,
  21. Int32,
  22. UInt64,
  23. Int64,
  24. Float32,
  25. Float64,
  26. }
  27. private IntegerType _integerType;
  28. private ulong _data;
  29. public ushort WordCount { get; private set; }
  30. public LiteralInteger() { }
  31. private static LiteralInteger New()
  32. {
  33. return _pool.Allocate();
  34. }
  35. private LiteralInteger Set(ulong data, IntegerType integerType, ushort wordCount)
  36. {
  37. _data = data;
  38. _integerType = integerType;
  39. WordCount = wordCount;
  40. return this;
  41. }
  42. public static implicit operator LiteralInteger(int value) => New().Set((ulong)value, IntegerType.Int32, 1);
  43. public static implicit operator LiteralInteger(uint value) => New().Set(value, IntegerType.UInt32, 1);
  44. public static implicit operator LiteralInteger(long value) => New().Set((ulong)value, IntegerType.Int64, 2);
  45. public static implicit operator LiteralInteger(ulong value) => New().Set(value, IntegerType.UInt64, 2);
  46. public static implicit operator LiteralInteger(float value) => New().Set(BitConverter.SingleToUInt32Bits(value), IntegerType.Float32, 1);
  47. public static implicit operator LiteralInteger(double value) => New().Set(BitConverter.DoubleToUInt64Bits(value), IntegerType.Float64, 2);
  48. public static implicit operator LiteralInteger(Enum value) => New().Set((ulong)(int)(object)value, IntegerType.Int32, 1);
  49. // NOTE: this is not in the standard, but this is some syntax sugar useful in some instructions (TypeInt ect)
  50. public static implicit operator LiteralInteger(bool value) => New().Set(Convert.ToUInt64(value), IntegerType.Int32, 1);
  51. public static LiteralInteger CreateForEnum<T>(T value) where T : Enum
  52. {
  53. return value;
  54. }
  55. public void WriteOperand(BinaryWriter writer)
  56. {
  57. if (WordCount == 1)
  58. {
  59. writer.Write((uint)_data);
  60. }
  61. else
  62. {
  63. writer.Write(_data);
  64. }
  65. }
  66. public override bool Equals(object obj)
  67. {
  68. return obj is LiteralInteger literalInteger && Equals(literalInteger);
  69. }
  70. public bool Equals(LiteralInteger cmpObj)
  71. {
  72. return Type == cmpObj.Type && _integerType == cmpObj._integerType && _data == cmpObj._data;
  73. }
  74. public override int GetHashCode()
  75. {
  76. return DeterministicHashCode.Combine(Type, _data);
  77. }
  78. public bool Equals(Operand obj)
  79. {
  80. return obj is LiteralInteger literalInteger && Equals(literalInteger);
  81. }
  82. public override string ToString() => $"{_integerType} {_data}";
  83. }
  84. }