CodeGenContext.cs 11 KB

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