CodeGenContext.cs 8.4 KB

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