Operand.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. internal ref ulong GetValueUnsafe()
  124. {
  125. return ref _data->Value;
  126. }
  127. internal void NumberLocal(int number)
  128. {
  129. if (Kind != OperandKind.LocalVariable)
  130. {
  131. throw new InvalidOperationException("The operand is not a local variable.");
  132. }
  133. Value = (ulong)number;
  134. }
  135. public void AddAssignment(Operation operation)
  136. {
  137. if (Kind == OperandKind.LocalVariable)
  138. {
  139. Add(operation, ref _data->Assignments, ref _data->AssignmentsCount, ref _data->AssignmentsCapacity);
  140. }
  141. else if (Kind == OperandKind.Memory)
  142. {
  143. MemoryOperand memOp = GetMemory();
  144. Operand addr = memOp.BaseAddress;
  145. Operand index = memOp.Index;
  146. if (addr != default)
  147. {
  148. Add(operation, ref addr._data->Assignments, ref addr._data->AssignmentsCount, ref addr._data->AssignmentsCapacity);
  149. }
  150. if (index != default)
  151. {
  152. Add(operation, ref index._data->Assignments, ref index._data->AssignmentsCount, ref index._data->AssignmentsCapacity);
  153. }
  154. }
  155. }
  156. public void RemoveAssignment(Operation operation)
  157. {
  158. if (Kind == OperandKind.LocalVariable)
  159. {
  160. Remove(operation, ref _data->Assignments, ref _data->AssignmentsCount);
  161. }
  162. else if (Kind == OperandKind.Memory)
  163. {
  164. MemoryOperand memOp = GetMemory();
  165. Operand addr = memOp.BaseAddress;
  166. Operand index = memOp.Index;
  167. if (addr != default)
  168. {
  169. Remove(operation, ref addr._data->Assignments, ref addr._data->AssignmentsCount);
  170. }
  171. if (index != default)
  172. {
  173. Remove(operation, ref index._data->Assignments, ref index._data->AssignmentsCount);
  174. }
  175. }
  176. }
  177. public void AddUse(Operation operation)
  178. {
  179. if (Kind == OperandKind.LocalVariable)
  180. {
  181. Add(operation, ref _data->Uses, ref _data->UsesCount, ref _data->UsesCapacity);
  182. }
  183. else if (Kind == OperandKind.Memory)
  184. {
  185. MemoryOperand memOp = GetMemory();
  186. Operand addr = memOp.BaseAddress;
  187. Operand index = memOp.Index;
  188. if (addr != default)
  189. {
  190. Add(operation, ref addr._data->Uses, ref addr._data->UsesCount, ref addr._data->UsesCapacity);
  191. }
  192. if (index != default)
  193. {
  194. Add(operation, ref index._data->Uses, ref index._data->UsesCount, ref index._data->UsesCapacity);
  195. }
  196. }
  197. }
  198. public void RemoveUse(Operation operation)
  199. {
  200. if (Kind == OperandKind.LocalVariable)
  201. {
  202. Remove(operation, ref _data->Uses, ref _data->UsesCount);
  203. }
  204. else if (Kind == OperandKind.Memory)
  205. {
  206. MemoryOperand memOp = GetMemory();
  207. Operand addr = memOp.BaseAddress;
  208. Operand index = memOp.Index;
  209. if (addr != default)
  210. {
  211. Remove(operation, ref addr._data->Uses, ref addr._data->UsesCount);
  212. }
  213. if (index != default)
  214. {
  215. Remove(operation, ref index._data->Uses, ref index._data->UsesCount);
  216. }
  217. }
  218. }
  219. private static void New<T>(ref T* data, ref ushort count, ref ushort capacity, ushort initialCapacity) where T : unmanaged
  220. {
  221. count = 0;
  222. capacity = initialCapacity;
  223. data = Allocators.References.Allocate<T>(initialCapacity);
  224. }
  225. private static void Add<T>(T item, ref T* data, ref ushort count, ref ushort capacity) where T : unmanaged
  226. {
  227. if (count < capacity)
  228. {
  229. data[(uint)count++] = item;
  230. return;
  231. }
  232. // Could not add item in the fast path, fallback onto the slow path.
  233. ExpandAdd(item, ref data, ref count, ref capacity);
  234. static void ExpandAdd(T item, ref T* data, ref ushort count, ref ushort capacity)
  235. {
  236. ushort newCount = checked((ushort)(count + 1));
  237. ushort newCapacity = (ushort)Math.Min(capacity * 2, ushort.MaxValue);
  238. var oldSpan = new Span<T>(data, count);
  239. capacity = newCapacity;
  240. data = Allocators.References.Allocate<T>(capacity);
  241. oldSpan.CopyTo(new Span<T>(data, count));
  242. data[count] = item;
  243. count = newCount;
  244. }
  245. }
  246. private static void Remove<T>(in T item, ref T* data, ref ushort count) where T : unmanaged
  247. {
  248. var span = new Span<T>(data, count);
  249. for (int i = 0; i < span.Length; i++)
  250. {
  251. if (EqualityComparer<T>.Default.Equals(span[i], item))
  252. {
  253. if (i + 1 < count)
  254. {
  255. span.Slice(i + 1).CopyTo(span.Slice(i));
  256. }
  257. count--;
  258. return;
  259. }
  260. }
  261. }
  262. public override int GetHashCode()
  263. {
  264. if (Kind == OperandKind.LocalVariable)
  265. {
  266. return base.GetHashCode();
  267. }
  268. else
  269. {
  270. return (int)Value ^ ((int)Kind << 16) ^ ((int)Type << 20);
  271. }
  272. }
  273. public bool Equals(Operand operand)
  274. {
  275. return operand._data == _data;
  276. }
  277. public override bool Equals(object obj)
  278. {
  279. return obj is Operand operand && Equals(operand);
  280. }
  281. public static bool operator ==(Operand a, Operand b)
  282. {
  283. return a.Equals(b);
  284. }
  285. public static bool operator !=(Operand a, Operand b)
  286. {
  287. return !a.Equals(b);
  288. }
  289. public static class Factory
  290. {
  291. private const int InternTableSize = 256;
  292. private const int InternTableProbeLength = 8;
  293. [ThreadStatic]
  294. private static Data* _internTable;
  295. private static Data* InternTable
  296. {
  297. get
  298. {
  299. if (_internTable == null)
  300. {
  301. _internTable = (Data*)NativeAllocator.Instance.Allocate((uint)sizeof(Data) * InternTableSize);
  302. // Make sure the table is zeroed.
  303. new Span<Data>(_internTable, InternTableSize).Clear();
  304. }
  305. return _internTable;
  306. }
  307. }
  308. private static Operand Make(OperandKind kind, OperandType type, ulong value, Symbol symbol = default)
  309. {
  310. Debug.Assert(kind != OperandKind.None);
  311. Data* data = null;
  312. // If constant or register, then try to look up in the intern table before allocating.
  313. if (kind == OperandKind.Constant || kind == OperandKind.Register)
  314. {
  315. uint hash = (uint)HashCode.Combine(kind, type, value);
  316. // Look in the next InternTableProbeLength slots for a match.
  317. for (uint i = 0; i < InternTableProbeLength; i++)
  318. {
  319. Operand interned = new();
  320. interned._data = &InternTable[(hash + i) % InternTableSize];
  321. // If slot matches the allocation request then return that slot.
  322. if (interned.Kind == kind && interned.Type == type && interned.Value == value && interned.Symbol == symbol)
  323. {
  324. return interned;
  325. }
  326. // Otherwise if the slot is not occupied, we store in that slot.
  327. else if (interned.Kind == OperandKind.None)
  328. {
  329. data = interned._data;
  330. break;
  331. }
  332. }
  333. }
  334. // If we could not get a slot from the intern table, we allocate somewhere else and store there.
  335. if (data == null)
  336. {
  337. data = Allocators.Operands.Allocate<Data>();
  338. }
  339. *data = default;
  340. Operand result = new();
  341. result._data = data;
  342. result.Value = value;
  343. result.Kind = kind;
  344. result.Type = type;
  345. if (kind != OperandKind.Memory)
  346. {
  347. result.Symbol = symbol;
  348. }
  349. // If local variable, then the use and def list is initialized with default sizes.
  350. if (kind == OperandKind.LocalVariable)
  351. {
  352. New(ref result._data->Assignments, ref result._data->AssignmentsCount, ref result._data->AssignmentsCapacity, 1);
  353. New(ref result._data->Uses, ref result._data->UsesCount, ref result._data->UsesCapacity, 4);
  354. }
  355. return result;
  356. }
  357. public static Operand Const(OperandType type, long value)
  358. {
  359. Debug.Assert(type is OperandType.I32 or OperandType.I64);
  360. return type == OperandType.I32 ? Const((int)value) : Const(value);
  361. }
  362. public static Operand Const(bool value)
  363. {
  364. return Const(value ? 1 : 0);
  365. }
  366. public static Operand Const(int value)
  367. {
  368. return Const((uint)value);
  369. }
  370. public static Operand Const(uint value)
  371. {
  372. return Make(OperandKind.Constant, OperandType.I32, value);
  373. }
  374. public static Operand Const(long value)
  375. {
  376. return Const(value, symbol: default);
  377. }
  378. public static Operand Const<T>(ref T reference, Symbol symbol = default)
  379. {
  380. return Const((long)Unsafe.AsPointer(ref reference), symbol);
  381. }
  382. public static Operand Const(long value, Symbol symbol)
  383. {
  384. return Make(OperandKind.Constant, OperandType.I64, (ulong)value, symbol);
  385. }
  386. public static Operand Const(ulong value)
  387. {
  388. return Make(OperandKind.Constant, OperandType.I64, value);
  389. }
  390. public static Operand ConstF(float value)
  391. {
  392. return Make(OperandKind.Constant, OperandType.FP32, (ulong)BitConverter.SingleToInt32Bits(value));
  393. }
  394. public static Operand ConstF(double value)
  395. {
  396. return Make(OperandKind.Constant, OperandType.FP64, (ulong)BitConverter.DoubleToInt64Bits(value));
  397. }
  398. public static Operand Label()
  399. {
  400. return Make(OperandKind.Label, OperandType.None, 0);
  401. }
  402. public static Operand Local(OperandType type)
  403. {
  404. return Make(OperandKind.LocalVariable, type, 0);
  405. }
  406. public static Operand Register(int index, RegisterType regType, OperandType type)
  407. {
  408. return Make(OperandKind.Register, type, (ulong)((int)regType << 24 | index));
  409. }
  410. public static Operand Undef()
  411. {
  412. return Make(OperandKind.Undefined, OperandType.None, 0);
  413. }
  414. public static Operand MemoryOp(
  415. OperandType type,
  416. Operand baseAddress,
  417. Operand index = default,
  418. Multiplier scale = Multiplier.x1,
  419. int displacement = 0)
  420. {
  421. Operand result = Make(OperandKind.Memory, type, 0);
  422. MemoryOperand memory = result.GetMemory();
  423. memory.BaseAddress = baseAddress;
  424. memory.Index = index;
  425. memory.Scale = scale;
  426. memory.Displacement = displacement;
  427. return result;
  428. }
  429. }
  430. }
  431. }