Instruction.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. namespace Spv.Generator
  5. {
  6. public sealed class Instruction : Operand, IEquatable<Instruction>
  7. {
  8. public const uint InvalidId = uint.MaxValue;
  9. public Specification.Op Opcode { get; private set; }
  10. private Instruction _resultType;
  11. private InstructionOperands _operands;
  12. public uint Id { get; set; }
  13. public Instruction() { }
  14. public void Set(Specification.Op opcode, uint id = InvalidId, Instruction resultType = null)
  15. {
  16. Opcode = opcode;
  17. Id = id;
  18. _resultType = resultType;
  19. _operands = new InstructionOperands();
  20. }
  21. public void SetId(uint id)
  22. {
  23. Id = id;
  24. }
  25. public OperandType Type => OperandType.Instruction;
  26. public ushort GetTotalWordCount()
  27. {
  28. ushort result = WordCount;
  29. if (Id != InvalidId)
  30. {
  31. result++;
  32. }
  33. if (_resultType != null)
  34. {
  35. result += _resultType.WordCount;
  36. }
  37. Span<Operand> operands = _operands.AsSpan();
  38. for (int i = 0; i < operands.Length; i++)
  39. {
  40. result += operands[i].WordCount;
  41. }
  42. return result;
  43. }
  44. public ushort WordCount => 1;
  45. public void AddOperand(Operand value)
  46. {
  47. Debug.Assert(value != null);
  48. _operands.Add(value);
  49. }
  50. public void AddOperand(Operand[] value)
  51. {
  52. foreach (Operand instruction in value)
  53. {
  54. AddOperand(instruction);
  55. }
  56. }
  57. public void AddOperand(LiteralInteger[] value)
  58. {
  59. foreach (LiteralInteger instruction in value)
  60. {
  61. AddOperand(instruction);
  62. }
  63. }
  64. public void AddOperand(LiteralInteger value)
  65. {
  66. AddOperand((Operand)value);
  67. }
  68. public void AddOperand(Instruction[] value)
  69. {
  70. foreach (Instruction instruction in value)
  71. {
  72. AddOperand(instruction);
  73. }
  74. }
  75. public void AddOperand(Instruction value)
  76. {
  77. AddOperand((Operand)value);
  78. }
  79. public void AddOperand(string value)
  80. {
  81. AddOperand(new LiteralString(value));
  82. }
  83. public void AddOperand<T>(T value) where T: Enum
  84. {
  85. AddOperand(LiteralInteger.CreateForEnum(value));
  86. }
  87. public void Write(BinaryWriter writer)
  88. {
  89. // Word 0
  90. writer.Write((ushort)Opcode);
  91. writer.Write(GetTotalWordCount());
  92. _resultType?.WriteOperand(writer);
  93. if (Id != InvalidId)
  94. {
  95. writer.Write(Id);
  96. }
  97. Span<Operand> operands = _operands.AsSpan();
  98. for (int i = 0; i < operands.Length; i++)
  99. {
  100. operands[i].WriteOperand(writer);
  101. }
  102. }
  103. public void WriteOperand(BinaryWriter writer)
  104. {
  105. Debug.Assert(Id != InvalidId);
  106. if (Id == InvalidId)
  107. {
  108. string methodToCall;
  109. if (Opcode == Specification.Op.OpVariable)
  110. {
  111. methodToCall = "AddLocalVariable or AddGlobalVariable";
  112. }
  113. else if (Opcode == Specification.Op.OpLabel)
  114. {
  115. methodToCall = "AddLabel";
  116. }
  117. else
  118. {
  119. throw new InvalidOperationException("Internal error");
  120. }
  121. throw new InvalidOperationException($"Id wasn't bound to the module, please make sure to call {methodToCall}");
  122. }
  123. writer.Write(Id);
  124. }
  125. public override bool Equals(object obj)
  126. {
  127. return obj is Instruction instruction && Equals(instruction);
  128. }
  129. public bool Equals(Instruction cmpObj)
  130. {
  131. bool result = Type == cmpObj.Type && Id == cmpObj.Id;
  132. if (result)
  133. {
  134. if (_resultType != null && cmpObj._resultType != null)
  135. {
  136. result &= _resultType.Equals(cmpObj._resultType);
  137. }
  138. else if (_resultType != null || cmpObj._resultType != null)
  139. {
  140. return false;
  141. }
  142. }
  143. if (result)
  144. {
  145. result &= EqualsContent(cmpObj);
  146. }
  147. return result;
  148. }
  149. public bool EqualsContent(Instruction cmpObj)
  150. {
  151. Span<Operand> thisOperands = _operands.AsSpan();
  152. Span<Operand> cmpOperands = cmpObj._operands.AsSpan();
  153. if (thisOperands.Length != cmpOperands.Length)
  154. {
  155. return false;
  156. }
  157. for (int i = 0; i < thisOperands.Length; i++)
  158. {
  159. if (!thisOperands[i].Equals(cmpOperands[i]))
  160. {
  161. return false;
  162. }
  163. }
  164. return true;
  165. }
  166. public bool EqualsResultType(Instruction cmpObj)
  167. {
  168. return _resultType.Opcode == cmpObj._resultType.Opcode && _resultType.EqualsContent(cmpObj._resultType);
  169. }
  170. public int GetHashCodeContent()
  171. {
  172. return DeterministicHashCode.Combine<Operand>(_operands.AsSpan());
  173. }
  174. public int GetHashCodeResultType()
  175. {
  176. return DeterministicHashCode.Combine(_resultType.Opcode, _resultType.GetHashCodeContent());
  177. }
  178. public override int GetHashCode()
  179. {
  180. return DeterministicHashCode.Combine(Opcode, Id, _resultType, DeterministicHashCode.Combine<Operand>(_operands.AsSpan()));
  181. }
  182. public bool Equals(Operand obj)
  183. {
  184. return obj is Instruction instruction && Equals(instruction);
  185. }
  186. }
  187. }