Instruction.cs 6.7 KB

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