InstructionOperands.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Spv.Generator
  4. {
  5. public struct InstructionOperands
  6. {
  7. private const int InternalCount = 5;
  8. public int Count;
  9. public Operand Operand1;
  10. public Operand Operand2;
  11. public Operand Operand3;
  12. public Operand Operand4;
  13. public Operand Operand5;
  14. public Operand[] Overflow;
  15. public Span<Operand> ToSpan()
  16. {
  17. if (Count > InternalCount)
  18. {
  19. return MemoryMarshal.CreateSpan(ref this.Overflow[0], Count);
  20. }
  21. else
  22. {
  23. return MemoryMarshal.CreateSpan(ref this.Operand1, Count);
  24. }
  25. }
  26. public void Add(Operand operand)
  27. {
  28. if (Count < InternalCount)
  29. {
  30. MemoryMarshal.CreateSpan(ref this.Operand1, Count + 1)[Count] = operand;
  31. Count++;
  32. }
  33. else
  34. {
  35. if (Overflow == null)
  36. {
  37. Overflow = new Operand[InternalCount * 2];
  38. MemoryMarshal.CreateSpan(ref this.Operand1, InternalCount).CopyTo(Overflow.AsSpan());
  39. }
  40. else if (Count == Overflow.Length)
  41. {
  42. Array.Resize(ref Overflow, Overflow.Length * 2);
  43. }
  44. Overflow[Count++] = operand;
  45. }
  46. }
  47. }
  48. }