CodeGenContext.cs 11 KB

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