CodeGenContext.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using ARMeilleure.CodeGen.Linking;
  2. using ARMeilleure.CodeGen.RegisterAllocators;
  3. using ARMeilleure.Common;
  4. using ARMeilleure.IntermediateRepresentation;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. namespace ARMeilleure.CodeGen.X86
  10. {
  11. class CodeGenContext
  12. {
  13. private const int ReservedBytesForJump = 1;
  14. private readonly Stream _stream;
  15. private readonly bool _relocatable;
  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 int CallArgsRegionSize { get; }
  21. public int XmmSaveRegionSize { get; }
  22. private readonly long[] _blockOffsets;
  23. private struct Jump
  24. {
  25. public bool IsConditional { get; }
  26. public X86Condition Condition { get; }
  27. public BasicBlock Target { get; }
  28. public long JumpPosition { get; }
  29. public long RelativeOffset { get; set; }
  30. public int InstSize { get; set; }
  31. public Jump(BasicBlock target, long jumpPosition, int instSize = 0)
  32. {
  33. IsConditional = false;
  34. Condition = 0;
  35. Target = target;
  36. JumpPosition = jumpPosition;
  37. RelativeOffset = 0;
  38. InstSize = instSize;
  39. }
  40. public Jump(X86Condition condition, BasicBlock target, long jumpPosition, int instSize = 0)
  41. {
  42. IsConditional = true;
  43. Condition = condition;
  44. Target = target;
  45. JumpPosition = jumpPosition;
  46. RelativeOffset = 0;
  47. InstSize = instSize;
  48. }
  49. }
  50. private readonly List<Jump> _jumps;
  51. private X86Condition _jNearCondition;
  52. private long _jNearPosition;
  53. private int _jNearLength;
  54. public CodeGenContext(Stream stream, AllocationResult allocResult, int maxCallArgs, int blocksCount, bool relocatable)
  55. {
  56. _stream = stream;
  57. _relocatable = relocatable;
  58. _blockOffsets = new long[blocksCount];
  59. _jumps = new List<Jump>();
  60. AllocResult = allocResult;
  61. Assembler = new Assembler(stream, relocatable);
  62. CallArgsRegionSize = GetCallArgsRegionSize(allocResult, maxCallArgs, out int xmmSaveRegionSize);
  63. XmmSaveRegionSize = xmmSaveRegionSize;
  64. }
  65. private static int GetCallArgsRegionSize(AllocationResult allocResult, int maxCallArgs, out int xmmSaveRegionSize)
  66. {
  67. // We need to add 8 bytes to the total size, as the call to this
  68. // function already pushed 8 bytes (the return address).
  69. int intMask = CallingConvention.GetIntCalleeSavedRegisters() & allocResult.IntUsedRegisters;
  70. int vecMask = CallingConvention.GetVecCalleeSavedRegisters() & allocResult.VecUsedRegisters;
  71. xmmSaveRegionSize = BitUtils.CountBits(vecMask) * 16;
  72. int calleeSaveRegionSize = BitUtils.CountBits(intMask) * 8 + xmmSaveRegionSize + 8;
  73. int argsCount = maxCallArgs;
  74. if (argsCount < 0)
  75. {
  76. // When the function has no calls, argsCount is -1.
  77. // In this case, we don't need to allocate the shadow space.
  78. argsCount = 0;
  79. }
  80. else if (argsCount < 4)
  81. {
  82. // The ABI mandates that the space for at least 4 arguments
  83. // is reserved on the stack (this is called shadow space).
  84. argsCount = 4;
  85. }
  86. int frameSize = calleeSaveRegionSize + allocResult.SpillRegionSize;
  87. // TODO: Instead of always multiplying by 16 (the largest possible size of a variable,
  88. // since a V128 has 16 bytes), we should calculate the exact size consumed by the
  89. // arguments passed to the called functions on the stack.
  90. int callArgsAndFrameSize = frameSize + argsCount * 16;
  91. // Ensure that the Stack Pointer will be aligned to 16 bytes.
  92. callArgsAndFrameSize = (callArgsAndFrameSize + 0xf) & ~0xf;
  93. return callArgsAndFrameSize - frameSize;
  94. }
  95. public void EnterBlock(BasicBlock block)
  96. {
  97. _blockOffsets[block.Index] = _stream.Position;
  98. CurrBlock = block;
  99. }
  100. public void JumpTo(BasicBlock target)
  101. {
  102. if (!_relocatable)
  103. {
  104. _jumps.Add(new Jump(target, _stream.Position));
  105. WritePadding(ReservedBytesForJump);
  106. }
  107. else
  108. {
  109. _jumps.Add(new Jump(target, _stream.Position, 5));
  110. WritePadding(5);
  111. }
  112. }
  113. public void JumpTo(X86Condition condition, BasicBlock target)
  114. {
  115. if (!_relocatable)
  116. {
  117. _jumps.Add(new Jump(condition, target, _stream.Position));
  118. WritePadding(ReservedBytesForJump);
  119. }
  120. else
  121. {
  122. _jumps.Add(new Jump(condition, target, _stream.Position, 6));
  123. WritePadding(6);
  124. }
  125. }
  126. public void JumpToNear(X86Condition condition)
  127. {
  128. _jNearCondition = condition;
  129. _jNearPosition = _stream.Position;
  130. _jNearLength = Assembler.GetJccLength(0, _relocatable);
  131. _stream.Seek(_jNearLength, SeekOrigin.Current);
  132. }
  133. public void JumpHere()
  134. {
  135. long currentPosition = _stream.Position;
  136. _stream.Seek(_jNearPosition, SeekOrigin.Begin);
  137. long offset = currentPosition - (_jNearPosition + _jNearLength);
  138. Debug.Assert(_jNearLength == Assembler.GetJccLength(offset, _relocatable), "Relative offset doesn't fit on near jump.");
  139. Assembler.Jcc(_jNearCondition, offset);
  140. _stream.Seek(currentPosition, SeekOrigin.Begin);
  141. }
  142. private void WritePadding(int size)
  143. {
  144. while (size-- > 0)
  145. {
  146. _stream.WriteByte(0);
  147. }
  148. }
  149. public (byte[], RelocInfo) GetCode()
  150. {
  151. // Write jump relative offsets.
  152. bool modified;
  153. do
  154. {
  155. modified = false;
  156. for (int index = 0; index < _jumps.Count; index++)
  157. {
  158. Jump jump = _jumps[index];
  159. long jumpTarget = _blockOffsets[jump.Target.Index];
  160. long offset = jumpTarget - jump.JumpPosition;
  161. if (!_relocatable)
  162. {
  163. if (offset < 0)
  164. {
  165. for (int index2 = index - 1; index2 >= 0; index2--)
  166. {
  167. Jump jump2 = _jumps[index2];
  168. if (jump2.JumpPosition < jumpTarget)
  169. {
  170. break;
  171. }
  172. offset -= jump2.InstSize - ReservedBytesForJump;
  173. }
  174. }
  175. else
  176. {
  177. for (int index2 = index + 1; index2 < _jumps.Count; index2++)
  178. {
  179. Jump jump2 = _jumps[index2];
  180. if (jump2.JumpPosition >= jumpTarget)
  181. {
  182. break;
  183. }
  184. offset += jump2.InstSize - ReservedBytesForJump;
  185. }
  186. offset -= ReservedBytesForJump;
  187. }
  188. if (jump.IsConditional)
  189. {
  190. jump.InstSize = Assembler.GetJccLength(offset);
  191. }
  192. else
  193. {
  194. jump.InstSize = Assembler.GetJmpLength(offset);
  195. }
  196. // The jump is relative to the next instruction, not the current one.
  197. // Since we didn't know the next instruction address when calculating
  198. // the offset (as the size of the current jump instruction was not known),
  199. // we now need to compensate the offset with the jump instruction size.
  200. // It's also worth noting that:
  201. // - This is only needed for backward jumps.
  202. // - The GetJmpLength and GetJccLength also compensates the offset
  203. // internally when computing the jump instruction size.
  204. if (offset < 0)
  205. {
  206. offset -= jump.InstSize;
  207. }
  208. }
  209. else
  210. {
  211. offset -= jump.InstSize;
  212. }
  213. if (jump.RelativeOffset != offset)
  214. {
  215. modified = true;
  216. }
  217. jump.RelativeOffset = offset;
  218. _jumps[index] = jump;
  219. }
  220. }
  221. while (modified);
  222. // Write the code, ignoring the dummy bytes after jumps, into a new stream.
  223. _stream.Seek(0, SeekOrigin.Begin);
  224. using (MemoryStream codeStream = new MemoryStream())
  225. {
  226. Assembler assembler = new Assembler(codeStream, _relocatable);
  227. for (int index = 0; index < _jumps.Count; index++)
  228. {
  229. Jump jump = _jumps[index];
  230. Span<byte> buffer = new byte[jump.JumpPosition - _stream.Position];
  231. _stream.Read(buffer);
  232. _stream.Seek(!_relocatable ? ReservedBytesForJump : jump.InstSize, SeekOrigin.Current);
  233. codeStream.Write(buffer);
  234. if (jump.IsConditional)
  235. {
  236. assembler.Jcc(jump.Condition, jump.RelativeOffset);
  237. }
  238. else
  239. {
  240. assembler.Jmp(jump.RelativeOffset);
  241. }
  242. }
  243. _stream.CopyTo(codeStream);
  244. var code = codeStream.ToArray();
  245. var relocInfo = Assembler.HasRelocs
  246. ? new RelocInfo(Assembler.Relocs.ToArray())
  247. : RelocInfo.Empty;
  248. return (code, relocInfo);
  249. }
  250. }
  251. }
  252. }