abi.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ==================
  2. System V ABI AMD64
  3. ==================
  4. This document describes concisely the subset of the amd64
  5. ABI as it is implemented in QBE. The subset can handle
  6. correctly arbitrary standard C-like structs containing
  7. float and integer types. Structs that have unaligned
  8. members are also supported through opaque types, see
  9. the IL description document for more information about
  10. them.
  11. - ABI Subset Implemented
  12. ------------------------
  13. Data classes of interest as defined by the ABI:
  14. * INTEGER
  15. * SSE
  16. * MEMORY
  17. ~ Classification
  18. 1. The size of each argument gets rounded up to eightbytes.
  19. (It keeps the stack always 8 bytes aligned.)
  20. 2. _Bool, char, short, int, long, long long and pointers
  21. are in the INTEGER class. In the context of QBE, it
  22. means that 'l' and 'w' are in the INTEGER class.
  23. 3. float and double are in the SSE class. In the context
  24. of QBE, it means that 's' and 'd' are in the SSE class.
  25. 4. If the size of an object is larger than two eightbytes
  26. or if contains unaligned fields, it has class MEMORY.
  27. In the context of QBE, those are big aggregate types
  28. and opaque types.
  29. 5. Otherwise, recursively classify fields and determine
  30. the class of the two eightbytes using the classes of
  31. their components. If any is INTEGER the result is
  32. INTEGER, otherwise the result is SSE.
  33. ~ Passing
  34. * Classify arguments in order.
  35. * INTEGER arguments use in order `%rdi` `%rsi` `%rdx`
  36. `%rcx` `%r8` `%r9`.
  37. * SSE arguments use in order `%xmm0` - `%xmm7`.
  38. * MEMORY gets passed on the stack. They are "pushed"
  39. in the right-to-left order, so from the callee's
  40. point of view, the left-most argument appears first
  41. on the stack.
  42. * When we run out of registers for an aggregate, revert
  43. the assignment for the first eightbytes and pass it
  44. on the stack.
  45. * When all registers are taken, write arguments on the
  46. stack from right to left.
  47. * When calling a variadic function, %al stores the number
  48. of vector registers used to pass arguments (it must be
  49. an upper bound and does not have to be exact).
  50. * Registers `%rbx`, `%r12` - `%r15` are callee-save.
  51. ~ Returning
  52. * Classify the return type.
  53. * Use `%rax` and `%rdx` in order for INTEGER return
  54. values.
  55. * Use `%xmm0` and `%xmm1` in order for SSE return values.
  56. * If the return value's class is MEMORY, the first
  57. argument of the function `%rdi` was a pointer to an
  58. area big enough to fit the return value. The function
  59. writes the return value there and returns the address
  60. (that was in `%rdi`) in `%rax`.
  61. - Alignment on the Stack
  62. ------------------------
  63. The ABI is unclear on the alignment requirement of the
  64. stack. What must be ensured is that, right before
  65. executing a 'call' instruction, the stack pointer `%rsp`
  66. is aligned on 16 bytes. On entry of the called
  67. function, the stack pointer is 8 modulo 16. Since most
  68. functions will have a prelude pushing `%rbp`, the frame
  69. pointer, upon entry of the body code of the function is
  70. also aligned on 16 bytes (== 0 mod 16).
  71. Here is a diagram of the stack layout after a call from
  72. g() to f().
  73. | |
  74. | g() locals |
  75. +-------------+
  76. ^ | | \
  77. | | stack arg 2 | '
  78. | |xxxxxxxxxxxxx| | f()'s MEMORY
  79. growing | +-------------+ | arguments
  80. addresses | | stack arg 1 | ,
  81. | |xxxxxxxxxxxxx| /
  82. | +-------------+ -> 0 mod 16
  83. | | ret addr |
  84. +-------------+
  85. | saved %rbp |
  86. +-------------+ -> f()'s %rbp
  87. | f() locals | 0 mod 16
  88. | ... |
  89. -> %rsp
  90. Legend:
  91. * `xxxxx` Optional padding.
  92. - Remarks
  93. ---------
  94. * A struct can be returned in registers in one of three
  95. ways. Either `%rax`, `%rdx` are used, or `%xmm0`,
  96. `%xmm1`, or finally `%rax`, `%xmm0`. The last case
  97. happens when a struct is returned with one half
  98. classified as INTEGER and the other as SSE. This
  99. is a consequence of the <@Returning> section above.
  100. * The size of the arguments area of the stack needs to
  101. be computed first, then arguments are packed starting
  102. from the bottom of the argument area, respecting
  103. alignment constraints. The ABI mentions "pushing"
  104. arguments in right-to-left order, but I think it's a
  105. mistaken view because of the alignment constraints.
  106. Example: If three 8 bytes MEMORY arguments are passed
  107. to the callee and the caller's stack pointer is 16 bytes
  108. algined, the layout will be like this.
  109. +-------------+
  110. |xxxxxxxxxxxxx| padding
  111. | stack arg 3 |
  112. | stack arg 2 |
  113. | stack arg 1 |
  114. +-------------+ -> 0 mod 16
  115. The padding must not be at the end of the stack area.
  116. A "pushing" logic would put it at the end.