smartalign.mac 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ;; --------------------------------------------------------------------------
  2. ;;
  3. ;; Copyright 1996-2017 The NASM Authors - All Rights Reserved
  4. ;; See the file AUTHORS included with the NASM distribution for
  5. ;; the specific copyright holders.
  6. ;;
  7. ;; Redistribution and use in source and binary forms, with or without
  8. ;; modification, are permitted provided that the following
  9. ;; conditions are met:
  10. ;;
  11. ;; * Redistributions of source code must retain the above copyright
  12. ;; notice, this list of conditions and the following disclaimer.
  13. ;; * Redistributions in binary form must reproduce the above
  14. ;; copyright notice, this list of conditions and the following
  15. ;; disclaimer in the documentation and/or other materials provided
  16. ;; with the distribution.
  17. ;;
  18. ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. ;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. ;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. ;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. ;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. ;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. ;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. ;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. ;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ;; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. ;; EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ;;
  32. ;; --------------------------------------------------------------------------
  33. ;
  34. ; Smart alignment macros
  35. ;
  36. USE: smartalign
  37. %imacro alignmode 1-2.nolist
  38. %ifidni %1,nop
  39. %define __ALIGN_JMP_THRESHOLD__ 16
  40. %define __ALIGN_16BIT_1B__ 0x90
  41. %define __ALIGN_16BIT_GROUP__ 1
  42. %define __ALIGN_32BIT_1B__ 0x90
  43. %define __ALIGN_32BIT_GROUP__ 1
  44. %define __ALIGN_64BIT_1B__ 0x90
  45. %define __ALIGN_64BIT_GROUP__ 1
  46. %elifidni %1,generic
  47. %define __ALIGN_JMP_THRESHOLD__ 8
  48. %define __ALIGN_16BIT_1B__ 0x90
  49. %define __ALIGN_16BIT_2B__ 0x89,0xf6
  50. %define __ALIGN_16BIT_3B__ 0x8d,0x74,0x00
  51. %define __ALIGN_16BIT_4B__ 0x8d,0xb4,0x00,0x00
  52. %define __ALIGN_16BIT_5B__ 0x8d,0xb4,0x00,0x00,0x90
  53. %define __ALIGN_16BIT_6B__ 0x8d,0xb4,0x00,0x00,0x89,0xff
  54. %define __ALIGN_16BIT_7B__ 0x8d,0xb4,0x00,0x00,0x8d,0x7d,0x00
  55. %define __ALIGN_16BIT_8B__ 0x8d,0xb4,0x00,0x00,0x8d,0xbd,0x00,0x00
  56. %define __ALIGN_16BIT_GROUP__ 8
  57. %define __ALIGN_32BIT_1B__ 0x90
  58. %define __ALIGN_32BIT_2B__ 0x89,0xf6
  59. %define __ALIGN_32BIT_3B__ 0x8d,0x76,0x00
  60. %define __ALIGN_32BIT_4B__ 0x8d,0x74,0x26,0x00
  61. %define __ALIGN_32BIT_5B__ 0x90,0x8d,0x74,0x26,0x00
  62. %define __ALIGN_32BIT_6B__ 0x8d,0xb6,0x00,0x00,0x00,0x00
  63. %define __ALIGN_32BIT_7B__ 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
  64. %define __ALIGN_32BIT_GROUP__ 7
  65. %define __ALIGN_64BIT_1B__ 0x90
  66. %define __ALIGN_64BIT_2B__ 0x66,0x90
  67. %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
  68. %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
  69. %define __ALIGN_64BIT_GROUP__ 4
  70. %elifidni %1,k8
  71. %define __ALIGN_JMP_THRESHOLD__ 16
  72. %define __ALIGN_16BIT_1B__ 0x90
  73. %define __ALIGN_16BIT_2B__ 0x66,0x90
  74. %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
  75. %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
  76. %define __ALIGN_16BIT_GROUP__ 4
  77. %define __ALIGN_32BIT_1B__ 0x90
  78. %define __ALIGN_32BIT_2B__ 0x66,0x90
  79. %define __ALIGN_32BIT_3B__ 0x66,0x66,0x90
  80. %define __ALIGN_32BIT_4B__ 0x66,0x66,0x66,0x90
  81. %define __ALIGN_32BIT_GROUP__ 4
  82. %define __ALIGN_64BIT_1B__ 0x90
  83. %define __ALIGN_64BIT_2B__ 0x66,0x90
  84. %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
  85. %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
  86. %define __ALIGN_64BIT_GROUP__ 4
  87. %elifidni %1,k7
  88. %define __ALIGN_JMP_THRESHOLD__ 16
  89. %define __ALIGN_16BIT_1B__ 0x90
  90. %define __ALIGN_16BIT_2B__ 0x66,0x90
  91. %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
  92. %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
  93. %define __ALIGN_64BIT_GROUP__ 4
  94. %define __ALIGN_32BIT_1B__ 0x90
  95. %define __ALIGN_32BIT_2B__ 0x8b,0xc0
  96. %define __ALIGN_32BIT_3B__ 0x8d,0x04,0x20
  97. %define __ALIGN_32BIT_4B__ 0x8d,0x44,0x20,0x00
  98. %define __ALIGN_32BIT_5B__ 0x8d,0x44,0x20,0x00,0x90
  99. %define __ALIGN_32BIT_6B__ 0x8d,0x80,0x00,0x00,0x00,0x00
  100. %define __ALIGN_32BIT_7B__ 0x8d,0x04,0x05,0x00,0x00,0x00,0x00
  101. %define __ALIGN_32BIT_GROUP__ 7
  102. %define __ALIGN_64BIT_1B__ 0x90
  103. %define __ALIGN_64BIT_2B__ 0x66,0x90
  104. %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
  105. %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
  106. %define __ALIGN_64BIT_GROUP__ 4
  107. %elifidni %1,p6
  108. %define __ALIGN_JMP_THRESHOLD__ 16
  109. %define __ALIGN_16BIT_1B__ 0x90
  110. %define __ALIGN_16BIT_2B__ 0x66,0x90
  111. %define __ALIGN_16BIT_3B__ 0x0f,0x1f,0x00
  112. %define __ALIGN_16BIT_4B__ 0x0f,0x1f,0x40,0x00
  113. %define __ALIGN_16BIT_GROUP__ 4
  114. %define __ALIGN_32BIT_1B__ 0x90
  115. %define __ALIGN_32BIT_2B__ 0x66,0x90
  116. %define __ALIGN_32BIT_3B__ 0x0f,0x1f,0x00
  117. %define __ALIGN_32BIT_4B__ 0x0f,0x1f,0x40,0x00
  118. %define __ALIGN_32BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
  119. %define __ALIGN_32BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
  120. %define __ALIGN_32BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
  121. %define __ALIGN_32BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
  122. %define __ALIGN_32BIT_GROUP__ 8
  123. %define __ALIGN_64BIT_1B__ 0x90
  124. %define __ALIGN_64BIT_2B__ 0x66,0x90
  125. %define __ALIGN_64BIT_3B__ 0x0f,0x1f,0x00
  126. %define __ALIGN_64BIT_4B__ 0x0f,0x1f,0x40,0x00
  127. %define __ALIGN_64BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
  128. %define __ALIGN_64BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
  129. %define __ALIGN_64BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
  130. %define __ALIGN_64BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
  131. %define __ALIGN_64BIT_GROUP__ 8
  132. %else
  133. %error unknown alignment mode: %1
  134. %endif
  135. %ifnempty %2
  136. %ifidni %2,nojmp
  137. %xdefine __ALIGN_JMP_THRESHOLD__ -1
  138. %else
  139. %xdefine __ALIGN_JMP_THRESHOLD__ %2
  140. %endif
  141. %endif
  142. %xdefine __ALIGNMODE__ %1,__ALIGN_JMP_THRESHOLD__
  143. %endmacro
  144. %unimacro align 1-2+.nolist
  145. %imacro align 1-2+.nolist
  146. sectalign %1 ; align a segment as well
  147. %ifnempty %2
  148. times (((%1) - (($-$$) % (%1))) % (%1)) %2
  149. %elif __PASS__ == 0 || __PASS__ == 3
  150. times (((%1) - (($-$$) % (%1))) % (%1)) nop
  151. %else
  152. %push
  153. %assign %$pad (((%1) - (($-$$) % (%1))) % (%1))
  154. %if __ALIGN_JMP_THRESHOLD__ != -1 && %$pad > __ALIGN_JMP_THRESHOLD__
  155. jmp %$end
  156. ; We can't re-use %$pad here as $ will have changed!
  157. times (((%1) - (($-$$) % (%1))) % (%1)) nop
  158. %else
  159. times (%$pad / __ALIGN_%[__BITS__]BIT_GROUP__) \
  160. db __ALIGN_%[__BITS__]BIT_%[__ALIGN_%[__BITS__]BIT_GROUP__]B__
  161. %assign %$pad %$pad % __ALIGN_%[__BITS__]BIT_GROUP__
  162. %if %$pad > 0
  163. db __ALIGN_%[__BITS__]BIT_%[%$pad]B__
  164. %endif
  165. %endif
  166. %$end:
  167. %pop
  168. %endif
  169. %endmacro
  170. alignmode generic