CodeGenContext.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using ARMeilleure.CodeGen.Linking;
  2. using ARMeilleure.CodeGen.RegisterAllocators;
  3. using ARMeilleure.IntermediateRepresentation;
  4. using Ryujinx.Common.Memory;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. namespace ARMeilleure.CodeGen.Arm64
  9. {
  10. class CodeGenContext
  11. {
  12. private const int BccInstLength = 4;
  13. private const int CbnzInstLength = 4;
  14. private const int LdrLitInstLength = 4;
  15. private Stream _stream;
  16. public int StreamOffset => (int)_stream.Length;
  17. public AllocationResult AllocResult { get; }
  18. public Assembler Assembler { get; }
  19. public BasicBlock CurrBlock { get; private set; }
  20. public bool HasCall { get; }
  21. public int CallArgsRegionSize { get; }
  22. public int FpLrSaveRegionSize { get; }
  23. private readonly Dictionary<BasicBlock, long> _visitedBlocks;
  24. private readonly Dictionary<BasicBlock, List<(ArmCondition Condition, long BranchPos)>> _pendingBranches;
  25. private struct ConstantPoolEntry
  26. {
  27. public readonly int Offset;
  28. public readonly Symbol Symbol;
  29. public readonly List<(Operand, int)> LdrOffsets;
  30. public ConstantPoolEntry(int offset, Symbol symbol)
  31. {
  32. Offset = offset;
  33. Symbol = symbol;
  34. LdrOffsets = new List<(Operand, int)>();
  35. }
  36. }
  37. private readonly Dictionary<ulong, ConstantPoolEntry> _constantPool;
  38. private bool _constantPoolWritten;
  39. private long _constantPoolOffset;
  40. private ArmCondition _jNearCondition;
  41. private Operand _jNearValue;
  42. private long _jNearPosition;
  43. private readonly bool _relocatable;
  44. public CodeGenContext(AllocationResult allocResult, int maxCallArgs, int blocksCount, bool relocatable)
  45. {
  46. _stream = MemoryStreamManager.Shared.GetStream();
  47. AllocResult = allocResult;
  48. Assembler = new Assembler(_stream);
  49. bool hasCall = maxCallArgs >= 0;
  50. HasCall = hasCall;
  51. if (maxCallArgs < 0)
  52. {
  53. maxCallArgs = 0;
  54. }
  55. CallArgsRegionSize = maxCallArgs * 16;
  56. FpLrSaveRegionSize = hasCall ? 16 : 0;
  57. _visitedBlocks = new Dictionary<BasicBlock, long>();
  58. _pendingBranches = new Dictionary<BasicBlock, List<(ArmCondition, long)>>();
  59. _constantPool = new Dictionary<ulong, ConstantPoolEntry>();
  60. _relocatable = relocatable;
  61. }
  62. public void EnterBlock(BasicBlock block)
  63. {
  64. CurrBlock = block;
  65. long target = _stream.Position;
  66. if (_pendingBranches.TryGetValue(block, out var list))
  67. {
  68. foreach (var tuple in list)
  69. {
  70. _stream.Seek(tuple.BranchPos, SeekOrigin.Begin);
  71. WriteBranch(tuple.Condition, target);
  72. }
  73. _stream.Seek(target, SeekOrigin.Begin);
  74. _pendingBranches.Remove(block);
  75. }
  76. _visitedBlocks.Add(block, target);
  77. }
  78. public void JumpTo(BasicBlock target)
  79. {
  80. JumpTo(ArmCondition.Al, target);
  81. }
  82. public void JumpTo(ArmCondition condition, BasicBlock target)
  83. {
  84. if (_visitedBlocks.TryGetValue(target, out long offset))
  85. {
  86. WriteBranch(condition, offset);
  87. }
  88. else
  89. {
  90. if (!_pendingBranches.TryGetValue(target, out var list))
  91. {
  92. list = new List<(ArmCondition, long)>();
  93. _pendingBranches.Add(target, list);
  94. }
  95. list.Add((condition, _stream.Position));
  96. _stream.Seek(BccInstLength, SeekOrigin.Current);
  97. }
  98. }
  99. private void WriteBranch(ArmCondition condition, long to)
  100. {
  101. int imm = checked((int)(to - _stream.Position));
  102. if (condition != ArmCondition.Al)
  103. {
  104. Assembler.B(condition, imm);
  105. }
  106. else
  107. {
  108. Assembler.B(imm);
  109. }
  110. }
  111. public void JumpToNear(ArmCondition condition)
  112. {
  113. _jNearCondition = condition;
  114. _jNearPosition = _stream.Position;
  115. _stream.Seek(BccInstLength, SeekOrigin.Current);
  116. }
  117. public void JumpToNearIfNotZero(Operand value)
  118. {
  119. _jNearValue = value;
  120. _jNearPosition = _stream.Position;
  121. _stream.Seek(CbnzInstLength, SeekOrigin.Current);
  122. }
  123. public void JumpHere()
  124. {
  125. long currentPosition = _stream.Position;
  126. long offset = currentPosition - _jNearPosition;
  127. _stream.Seek(_jNearPosition, SeekOrigin.Begin);
  128. if (_jNearValue != default)
  129. {
  130. Assembler.Cbnz(_jNearValue, checked((int)offset));
  131. _jNearValue = default;
  132. }
  133. else
  134. {
  135. Assembler.B(_jNearCondition, checked((int)offset));
  136. }
  137. _stream.Seek(currentPosition, SeekOrigin.Begin);
  138. }
  139. public void ReserveRelocatableConstant(Operand rt, Symbol symbol, ulong value)
  140. {
  141. if (!_constantPool.TryGetValue(value, out ConstantPoolEntry cpe))
  142. {
  143. cpe = new ConstantPoolEntry(_constantPool.Count * sizeof(ulong), symbol);
  144. _constantPool.Add(value, cpe);
  145. }
  146. cpe.LdrOffsets.Add((rt, (int)_stream.Position));
  147. _stream.Seek(LdrLitInstLength, SeekOrigin.Current);
  148. }
  149. private long WriteConstantPool()
  150. {
  151. if (_constantPoolWritten)
  152. {
  153. return _constantPoolOffset;
  154. }
  155. long constantPoolBaseOffset = _stream.Position;
  156. foreach (ulong value in _constantPool.Keys)
  157. {
  158. WriteUInt64(value);
  159. }
  160. foreach (ConstantPoolEntry cpe in _constantPool.Values)
  161. {
  162. foreach ((Operand rt, int ldrOffset) in cpe.LdrOffsets)
  163. {
  164. _stream.Seek(ldrOffset, SeekOrigin.Begin);
  165. int absoluteOffset = checked((int)(constantPoolBaseOffset + cpe.Offset));
  166. int pcRelativeOffset = absoluteOffset - ldrOffset;
  167. Assembler.LdrLit(rt, pcRelativeOffset);
  168. }
  169. }
  170. _stream.Seek(constantPoolBaseOffset + _constantPool.Count * sizeof(ulong), SeekOrigin.Begin);
  171. _constantPoolOffset = constantPoolBaseOffset;
  172. _constantPoolWritten = true;
  173. return constantPoolBaseOffset;
  174. }
  175. public (byte[], RelocInfo) GetCode()
  176. {
  177. long constantPoolBaseOffset = WriteConstantPool();
  178. byte[] code = new byte[_stream.Length];
  179. long originalPosition = _stream.Position;
  180. _stream.Seek(0, SeekOrigin.Begin);
  181. _stream.Read(code, 0, code.Length);
  182. _stream.Seek(originalPosition, SeekOrigin.Begin);
  183. RelocInfo relocInfo;
  184. if (_relocatable)
  185. {
  186. RelocEntry[] relocs = new RelocEntry[_constantPool.Count];
  187. int index = 0;
  188. foreach (ConstantPoolEntry cpe in _constantPool.Values)
  189. {
  190. if (cpe.Symbol.Type != SymbolType.None)
  191. {
  192. int absoluteOffset = checked((int)(constantPoolBaseOffset + cpe.Offset));
  193. relocs[index++] = new RelocEntry(absoluteOffset, cpe.Symbol);
  194. }
  195. }
  196. if (index != relocs.Length)
  197. {
  198. Array.Resize(ref relocs, index);
  199. }
  200. relocInfo = new RelocInfo(relocs);
  201. }
  202. else
  203. {
  204. relocInfo = new RelocInfo(Array.Empty<RelocEntry>());
  205. }
  206. return (code, relocInfo);
  207. }
  208. private void WriteUInt64(ulong value)
  209. {
  210. _stream.WriteByte((byte)(value >> 0));
  211. _stream.WriteByte((byte)(value >> 8));
  212. _stream.WriteByte((byte)(value >> 16));
  213. _stream.WriteByte((byte)(value >> 24));
  214. _stream.WriteByte((byte)(value >> 32));
  215. _stream.WriteByte((byte)(value >> 40));
  216. _stream.WriteByte((byte)(value >> 48));
  217. _stream.WriteByte((byte)(value >> 56));
  218. }
  219. }
  220. }