CallingConvention.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. namespace ARMeilleure.CodeGen.X86
  3. {
  4. static class CallingConvention
  5. {
  6. private const int RegistersMask = 0xffff;
  7. public static int GetIntAvailableRegisters()
  8. {
  9. return RegistersMask & ~(1 << (int)X86Register.Rsp);
  10. }
  11. public static int GetVecAvailableRegisters()
  12. {
  13. return RegistersMask;
  14. }
  15. public static int GetIntCallerSavedRegisters()
  16. {
  17. if (GetCurrentCallConv() == CallConvName.Windows)
  18. {
  19. return (1 << (int)X86Register.Rax) |
  20. (1 << (int)X86Register.Rcx) |
  21. (1 << (int)X86Register.Rdx) |
  22. (1 << (int)X86Register.R8) |
  23. (1 << (int)X86Register.R9) |
  24. (1 << (int)X86Register.R10) |
  25. (1 << (int)X86Register.R11);
  26. }
  27. else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
  28. {
  29. return (1 << (int)X86Register.Rax) |
  30. (1 << (int)X86Register.Rcx) |
  31. (1 << (int)X86Register.Rdx) |
  32. (1 << (int)X86Register.Rsi) |
  33. (1 << (int)X86Register.Rdi) |
  34. (1 << (int)X86Register.R8) |
  35. (1 << (int)X86Register.R9) |
  36. (1 << (int)X86Register.R10) |
  37. (1 << (int)X86Register.R11);
  38. }
  39. }
  40. public static int GetVecCallerSavedRegisters()
  41. {
  42. if (GetCurrentCallConv() == CallConvName.Windows)
  43. {
  44. return (1 << (int)X86Register.Xmm0) |
  45. (1 << (int)X86Register.Xmm1) |
  46. (1 << (int)X86Register.Xmm2) |
  47. (1 << (int)X86Register.Xmm3) |
  48. (1 << (int)X86Register.Xmm4) |
  49. (1 << (int)X86Register.Xmm5);
  50. }
  51. else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
  52. {
  53. return RegistersMask;
  54. }
  55. }
  56. public static int GetIntCalleeSavedRegisters()
  57. {
  58. return GetIntCallerSavedRegisters() ^ RegistersMask;
  59. }
  60. public static int GetVecCalleeSavedRegisters()
  61. {
  62. return GetVecCallerSavedRegisters() ^ RegistersMask;
  63. }
  64. public static int GetArgumentsOnRegsCount()
  65. {
  66. return 4;
  67. }
  68. public static int GetIntArgumentsOnRegsCount()
  69. {
  70. return 6;
  71. }
  72. public static int GetVecArgumentsOnRegsCount()
  73. {
  74. return 8;
  75. }
  76. public static X86Register GetIntArgumentRegister(int index)
  77. {
  78. if (GetCurrentCallConv() == CallConvName.Windows)
  79. {
  80. switch (index)
  81. {
  82. case 0: return X86Register.Rcx;
  83. case 1: return X86Register.Rdx;
  84. case 2: return X86Register.R8;
  85. case 3: return X86Register.R9;
  86. }
  87. }
  88. else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
  89. {
  90. switch (index)
  91. {
  92. case 0: return X86Register.Rdi;
  93. case 1: return X86Register.Rsi;
  94. case 2: return X86Register.Rdx;
  95. case 3: return X86Register.Rcx;
  96. case 4: return X86Register.R8;
  97. case 5: return X86Register.R9;
  98. }
  99. }
  100. throw new ArgumentOutOfRangeException(nameof(index));
  101. }
  102. public static X86Register GetVecArgumentRegister(int index)
  103. {
  104. int count;
  105. if (GetCurrentCallConv() == CallConvName.Windows)
  106. {
  107. count = 4;
  108. }
  109. else /* if (GetCurrentCallConv() == CallConvName.SystemV) */
  110. {
  111. count = 8;
  112. }
  113. if ((uint)index < count)
  114. {
  115. return X86Register.Xmm0 + index;
  116. }
  117. throw new ArgumentOutOfRangeException(nameof(index));
  118. }
  119. public static X86Register GetIntReturnRegister()
  120. {
  121. return X86Register.Rax;
  122. }
  123. public static X86Register GetIntReturnRegisterHigh()
  124. {
  125. return X86Register.Rdx;
  126. }
  127. public static X86Register GetVecReturnRegister()
  128. {
  129. return X86Register.Xmm0;
  130. }
  131. public static CallConvName GetCurrentCallConv()
  132. {
  133. return OperatingSystem.IsWindows()
  134. ? CallConvName.Windows
  135. : CallConvName.SystemV;
  136. }
  137. }
  138. }