Operand.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using ARMeilleure.CodeGen.Linking;
  2. using ARMeilleure.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. namespace ARMeilleure.IntermediateRepresentation
  8. {
  9. unsafe struct Operand : IEquatable<Operand>
  10. {
  11. internal struct Data
  12. {
  13. public byte Kind;
  14. public byte Type;
  15. public byte SymbolType;
  16. public ushort AssignmentsCount;
  17. public ushort AssignmentsCapacity;
  18. public ushort UsesCount;
  19. public ushort UsesCapacity;
  20. public Operation* Assignments;
  21. public Operation* Uses;
  22. public ulong Value;
  23. public ulong SymbolValue;
  24. }
  25. private Data* _data;
  26. public OperandKind Kind
  27. {
  28. get => (OperandKind)_data->Kind;
  29. private set => _data->Kind = (byte)value;
  30. }
  31. public OperandType Type
  32. {
  33. get => (OperandType)_data->Type;
  34. private set => _data->Type = (byte)value;
  35. }
  36. public ulong Value
  37. {
  38. get => _data->Value;
  39. private set => _data->Value = value;
  40. }
  41. public Symbol Symbol
  42. {
  43. get
  44. {
  45. Debug.Assert(Kind != OperandKind.Memory);
  46. return new Symbol((SymbolType)_data->SymbolType, _data->SymbolValue);
  47. }
  48. private set
  49. {
  50. Debug.Assert(Kind != OperandKind.Memory);
  51. if (value.Type == SymbolType.None)
  52. {
  53. _data->SymbolType = (byte)SymbolType.None;
  54. }
  55. else
  56. {
  57. _data->SymbolType = (byte)value.Type;
  58. _data->SymbolValue = value.Value;
  59. }
  60. }
  61. }
  62. public ReadOnlySpan<Operation> Assignments
  63. {
  64. get
  65. {
  66. Debug.Assert(Kind != OperandKind.Memory);
  67. return new ReadOnlySpan<Operation>(_data->Assignments, _data->AssignmentsCount);
  68. }
  69. }
  70. public ReadOnlySpan<Operation> Uses
  71. {
  72. get
  73. {
  74. Debug.Assert(Kind != OperandKind.Memory);
  75. return new ReadOnlySpan<Operation>(_data->Uses, _data->UsesCount);
  76. }
  77. }
  78. public int UsesCount => _data->UsesCount;
  79. public int AssignmentsCount => _data->AssignmentsCount;
  80. public bool Relocatable => Symbol.Type != SymbolType.None;
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public Register GetRegister()
  83. {
  84. Debug.Assert(Kind == OperandKind.Register);
  85. return new Register((int)Value & 0xffffff, (RegisterType)(Value >> 24));
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public MemoryOperand GetMemory()
  89. {
  90. Debug.Assert(Kind == OperandKind.Memory);
  91. return new MemoryOperand(this);
  92. }
  93. public int GetLocalNumber()
  94. {
  95. Debug.Assert(Kind == OperandKind.LocalVariable);
  96. return (int)Value;
  97. }
  98. public byte AsByte()
  99. {
  100. return (byte)Value;
  101. }
  102. public short AsInt16()
  103. {
  104. return (short)Value;
  105. }
  106. public int AsInt32()
  107. {
  108. return (int)Value;
  109. }
  110. public long AsInt64()
  111. {
  112. return (long)Value;
  113. }
  114. public float AsFloat()
  115. {
  116. return BitConverter.Int32BitsToSingle((int)Value);
  117. }
  118. public double AsDouble()
  119. {
  120. return BitConverter.Int64BitsToDouble((long)Value);
  121. }
  122. internal ref ulong GetValueUnsafe()
  123. {
  124. return ref _data->Value;
  125. }
  126. internal void NumberLocal(int number)
  127. {
  128. if (Kind != OperandKind.LocalVariable)
  129. {
  130. throw new InvalidOperationException("The operand is not a local variable.");
  131. }
  132. Value = (ulong)number;
  133. }
  134. public void AddAssignment(Operation operation)
  135. {
  136. if (Kind == OperandKind.LocalVariable)
  137. {
  138. Add(operation, ref _data->Assignments, ref _data->AssignmentsCount, ref _data->AssignmentsCapacity);
  139. }
  140. else if (Kind == OperandKind.Memory)
  141. {
  142. MemoryOperand memOp = GetMemory();
  143. Operand addr = memOp.BaseAddress;
  144. Operand index = memOp.Index;
  145. if (addr != default)
  146. {
  147. Add(operation, ref addr._data->Assignments, ref addr._data->AssignmentsCount, ref addr._data->AssignmentsCapacity);
  148. }
  149. if (index != default)
  150. {
  151. Add(operation, ref index._data->Assignments, ref index._data->AssignmentsCount, ref index._data->AssignmentsCapacity);
  152. }
  153. }
  154. }
  155. public void RemoveAssignment(Operation operation)
  156. {
  157. if (Kind == OperandKind.LocalVariable)
  158. {
  159. Remove(operation, ref _data->Assignments, ref _data->AssignmentsCount);
  160. }
  161. else if (Kind == OperandKind.Memory)
  162. {
  163. MemoryOperand memOp = GetMemory();
  164. Operand addr = memOp.BaseAddress;
  165. Operand index = memOp.Index;
  166. if (addr != default)
  167. {
  168. Remove(operation, ref addr._data->Assignments, ref addr._data->AssignmentsCount);
  169. }
  170. if (index != default)
  171. {
  172. Remove(operation, ref index._data->Assignments, ref index._data->AssignmentsCount);
  173. }
  174. }
  175. }
  176. public void AddUse(Operation operation)
  177. {
  178. if (Kind == OperandKind.LocalVariable)
  179. {
  180. Add(operation, ref _data->Uses, ref _data->UsesCount, ref _data->UsesCapacity);
  181. }
  182. else if (Kind == OperandKind.Memory)
  183. {
  184. MemoryOperand memOp = GetMemory();
  185. Operand addr = memOp.BaseAddress;
  186. Operand index = memOp.Index;
  187. if (addr != default)
  188. {
  189. Add(operation, ref addr._data->Uses, ref addr._data->UsesCount, ref addr._data->UsesCapacity);
  190. }
  191. if (index != default)
  192. {
  193. Add(operation, ref index._data->Uses, ref index._data->UsesCount, ref index._data->UsesCapacity);
  194. }
  195. }
  196. }
  197. public void RemoveUse(Operation operation)
  198. {
  199. if (Kind == OperandKind.LocalVariable)
  200. {
  201. Remove(operation, ref _data->Uses, ref _data->UsesCount);
  202. }
  203. else if (Kind == OperandKind.Memory)
  204. {
  205. MemoryOperand memOp = GetMemory();
  206. Operand addr = memOp.BaseAddress;
  207. Operand index = memOp.Index;
  208. if (addr != default)
  209. {
  210. Remove(operation, ref addr._data->Uses, ref addr._data->UsesCount);
  211. }
  212. if (index != default)
  213. {
  214. Remove(operation, ref index._data->Uses, ref index._data->UsesCount);
  215. }
  216. }
  217. }
  218. private static void New<T>(ref T* data, ref ushort count, ref ushort capacity, ushort initialCapacity) where T : unmanaged
  219. {
  220. count = 0;
  221. capacity = initialCapacity;
  222. data = Allocators.References.Allocate<T>(initialCapacity);
  223. }
  224. private static void Add<T>(T item, ref T* data, ref ushort count, ref ushort capacity) where T : unmanaged
  225. {
  226. if (count < capacity)
  227. {
  228. data[(uint)count++] = item;
  229. return;
  230. }
  231. // Could not add item in the fast path, fallback onto the slow path.
  232. ExpandAdd(item, ref data, ref count, ref capacity);
  233. static void ExpandAdd(T item, ref T* data, ref ushort count, ref ushort capacity)
  234. {
  235. ushort newCount = checked((ushort)(count + 1));
  236. ushort newCapacity = (ushort)Math.Min(capacity * 2, ushort.MaxValue);
  237. var oldSpan = new Span<T>(data, count);
  238. capacity = newCapacity;
  239. data = Allocators.References.Allocate<T>(capacity);
  240. oldSpan.CopyTo(new Span<T>(data, count));
  241. data[count] = item;
  242. count = newCount;
  243. }
  244. }
  245. private static void Remove<T>(in T item, ref T* data, ref ushort count) where T : unmanaged
  246. {
  247. var span = new Span<T>(data, count);
  248. for (int i = 0; i < span.Length; i++)
  249. {
  250. if (EqualityComparer<T>.Default.Equals(span[i], item))
  251. {
  252. if (i + 1 < count)
  253. {
  254. span.Slice(i + 1).CopyTo(span.Slice(i));
  255. }
  256. count--;
  257. return;
  258. }
  259. }
  260. }
  261. public override int GetHashCode()
  262. {
  263. if (Kind == OperandKind.LocalVariable)
  264. {
  265. return base.GetHashCode();
  266. }
  267. else
  268. {
  269. return (int)Value ^ ((int)Kind << 16) ^ ((int)Type << 20);
  270. }
  271. }
  272. public bool Equals(Operand operand)
  273. {
  274. return operand._data == _data;
  275. }
  276. public override bool Equals(object obj)
  277. {
  278. return obj is Operand operand && Equals(operand);
  279. }
  280. public static bool operator ==(Operand a, Operand b)
  281. {
  282. return a.Equals(b);
  283. }
  284. public static bool operator !=(Operand a, Operand b)
  285. {
  286. return !a.Equals(b);
  287. }
  288. public static class Factory
  289. {
  290. private const int InternTableSize = 256;
  291. private const int InternTableProbeLength = 8;
  292. [ThreadStatic]
  293. private static Data* _internTable;
  294. private static Data* InternTable
  295. {
  296. get
  297. {
  298. if (_internTable == null)
  299. {
  300. _internTable = (Data*)NativeAllocator.Instance.Allocate((uint)sizeof(Data) * InternTableSize);
  301. // Make sure the table is zeroed.
  302. new Span<Data>(_internTable, InternTableSize).Clear();
  303. }
  304. return _internTable;
  305. }
  306. }
  307. private static Operand Make(OperandKind kind, OperandType type, ulong value, Symbol symbol = default)
  308. {
  309. Debug.Assert(kind != OperandKind.None);
  310. Data* data = null;
  311. // If constant or register, then try to look up in the intern table before allocating.
  312. if (kind == OperandKind.Constant || kind == OperandKind.Register)
  313. {
  314. uint hash = (uint)HashCode.Combine(kind, type, value);
  315. // Look in the next InternTableProbeLength slots for a match.
  316. for (uint i = 0; i < InternTableProbeLength; i++)
  317. {
  318. Operand interned = new();
  319. interned._data = &InternTable[(hash + i) % InternTableSize];
  320. // If slot matches the allocation request then return that slot.
  321. if (interned.Kind == kind && interned.Type == type && interned.Value == value && interned.Symbol == symbol)
  322. {
  323. return interned;
  324. }
  325. // Otherwise if the slot is not occupied, we store in that slot.
  326. else if (interned.Kind == OperandKind.None)
  327. {
  328. data = interned._data;
  329. break;
  330. }
  331. }
  332. }
  333. // If we could not get a slot from the intern table, we allocate somewhere else and store there.
  334. if (data == null)
  335. {
  336. data = Allocators.Operands.Allocate<Data>();
  337. }
  338. *data = default;
  339. Operand result = new();
  340. result._data = data;
  341. result.Value = value;
  342. result.Kind = kind;
  343. result.Type = type;
  344. if (kind != OperandKind.Memory)
  345. {
  346. result.Symbol = symbol;
  347. }
  348. // If local variable, then the use and def list is initialized with default sizes.
  349. if (kind == OperandKind.LocalVariable)
  350. {
  351. New(ref result._data->Assignments, ref result._data->AssignmentsCount, ref result._data->AssignmentsCapacity, 1);
  352. New(ref result._data->Uses, ref result._data->UsesCount, ref result._data->UsesCapacity, 4);
  353. }
  354. return result;
  355. }
  356. public static Operand Const(OperandType type, long value)
  357. {
  358. Debug.Assert(type is OperandType.I32 or OperandType.I64);
  359. return type == OperandType.I32 ? Const((int)value) : Const(value);
  360. }
  361. public static Operand Const(bool value)
  362. {
  363. return Const(value ? 1 : 0);
  364. }
  365. public static Operand Const(int value)
  366. {
  367. return Const((uint)value);
  368. }
  369. public static Operand Const(uint value)
  370. {
  371. return Make(OperandKind.Constant, OperandType.I32, value);
  372. }
  373. public static Operand Const(long value)
  374. {
  375. return Const(value, symbol: default);
  376. }
  377. public static Operand Const<T>(ref T reference, Symbol symbol = default)
  378. {
  379. return Const((long)Unsafe.AsPointer(ref reference), symbol);
  380. }
  381. public static Operand Const(long value, Symbol symbol)
  382. {
  383. return Make(OperandKind.Constant, OperandType.I64, (ulong)value, symbol);
  384. }
  385. public static Operand Const(ulong value)
  386. {
  387. return Make(OperandKind.Constant, OperandType.I64, value);
  388. }
  389. public static Operand ConstF(float value)
  390. {
  391. return Make(OperandKind.Constant, OperandType.FP32, (ulong)BitConverter.SingleToInt32Bits(value));
  392. }
  393. public static Operand ConstF(double value)
  394. {
  395. return Make(OperandKind.Constant, OperandType.FP64, (ulong)BitConverter.DoubleToInt64Bits(value));
  396. }
  397. public static Operand Label()
  398. {
  399. return Make(OperandKind.Label, OperandType.None, 0);
  400. }
  401. public static Operand Local(OperandType type)
  402. {
  403. return Make(OperandKind.LocalVariable, type, 0);
  404. }
  405. public static Operand Register(int index, RegisterType regType, OperandType type)
  406. {
  407. return Make(OperandKind.Register, type, (ulong)((int)regType << 24 | index));
  408. }
  409. public static Operand Undef()
  410. {
  411. return Make(OperandKind.Undefined, OperandType.None, 0);
  412. }
  413. public static Operand MemoryOp(
  414. OperandType type,
  415. Operand baseAddress,
  416. Operand index = default,
  417. Multiplier scale = Multiplier.x1,
  418. int displacement = 0)
  419. {
  420. Operand result = Make(OperandKind.Memory, type, 0);
  421. MemoryOperand memory = result.GetMemory();
  422. memory.BaseAddress = baseAddress;
  423. memory.Index = index;
  424. memory.Scale = scale;
  425. memory.Displacement = displacement;
  426. return result;
  427. }
  428. }
  429. }
  430. }