| 1 |
;; SPDX-License-Identifier: BSD-2-Clause |
| 2 |
;; Copyright 1996-2017 The NASM Authors - All Rights Reserved |
| 3 |
|
| 4 |
; |
| 5 |
; Smart alignment macros |
| 6 |
; |
| 7 |
USE: smartalign |
| 8 |
|
| 9 |
%imacro alignmode 1-2.nolist |
| 10 |
%ifidni %1,nop |
| 11 |
%define __?ALIGN_JMP_THRESHOLD?__ 16 |
| 12 |
|
| 13 |
%define __?ALIGN_16BIT_1B?__ 0x90 |
| 14 |
%define __?ALIGN_16BIT_GROUP?__ 1 |
| 15 |
|
| 16 |
%define __?ALIGN_32BIT_1B?__ 0x90 |
| 17 |
%define __?ALIGN_32BIT_GROUP?__ 1 |
| 18 |
|
| 19 |
%define __?ALIGN_64BIT_1B?__ 0x90 |
| 20 |
%define __?ALIGN_64BIT_GROUP?__ 1 |
| 21 |
%elifidni %1,generic |
| 22 |
%define __?ALIGN_JMP_THRESHOLD?__ 8 |
| 23 |
|
| 24 |
%define __?ALIGN_16BIT_1B?__ 0x90 |
| 25 |
%define __?ALIGN_16BIT_2B?__ 0x89,0xf6 |
| 26 |
%define __?ALIGN_16BIT_3B?__ 0x8d,0x74,0x00 |
| 27 |
%define __?ALIGN_16BIT_4B?__ 0x8d,0xb4,0x00,0x00 |
| 28 |
%define __?ALIGN_16BIT_5B?__ 0x8d,0xb4,0x00,0x00,0x90 |
| 29 |
%define __?ALIGN_16BIT_6B?__ 0x8d,0xb4,0x00,0x00,0x89,0xff |
| 30 |
%define __?ALIGN_16BIT_7B?__ 0x8d,0xb4,0x00,0x00,0x8d,0x7d,0x00 |
| 31 |
%define __?ALIGN_16BIT_8B?__ 0x8d,0xb4,0x00,0x00,0x8d,0xbd,0x00,0x00 |
| 32 |
%define __?ALIGN_16BIT_GROUP?__ 8 |
| 33 |
|
| 34 |
%define __?ALIGN_32BIT_1B?__ 0x90 |
| 35 |
%define __?ALIGN_32BIT_2B?__ 0x89,0xf6 |
| 36 |
%define __?ALIGN_32BIT_3B?__ 0x8d,0x76,0x00 |
| 37 |
%define __?ALIGN_32BIT_4B?__ 0x8d,0x74,0x26,0x00 |
| 38 |
%define __?ALIGN_32BIT_5B?__ 0x90,0x8d,0x74,0x26,0x00 |
| 39 |
%define __?ALIGN_32BIT_6B?__ 0x8d,0xb6,0x00,0x00,0x00,0x00 |
| 40 |
%define __?ALIGN_32BIT_7B?__ 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00 |
| 41 |
%define __?ALIGN_32BIT_GROUP?__ 7 |
| 42 |
|
| 43 |
%define __?ALIGN_64BIT_1B?__ 0x90 |
| 44 |
%define __?ALIGN_64BIT_2B?__ 0x66,0x90 |
| 45 |
%define __?ALIGN_64BIT_3B?__ 0x66,0x66,0x90 |
| 46 |
%define __?ALIGN_64BIT_4B?__ 0x66,0x66,0x66,0x90 |
| 47 |
%define __?ALIGN_64BIT_GROUP?__ 4 |
| 48 |
%elifidni %1,k8 |
| 49 |
%define __?ALIGN_JMP_THRESHOLD?__ 16 |
| 50 |
|
| 51 |
%define __?ALIGN_16BIT_1B?__ 0x90 |
| 52 |
%define __?ALIGN_16BIT_2B?__ 0x66,0x90 |
| 53 |
%define __?ALIGN_16BIT_3B?__ 0x66,0x66,0x90 |
| 54 |
%define __?ALIGN_16BIT_4B?__ 0x66,0x66,0x66,0x90 |
| 55 |
%define __?ALIGN_16BIT_GROUP?__ 4 |
| 56 |
|
| 57 |
%define __?ALIGN_32BIT_1B?__ 0x90 |
| 58 |
%define __?ALIGN_32BIT_2B?__ 0x66,0x90 |
| 59 |
%define __?ALIGN_32BIT_3B?__ 0x66,0x66,0x90 |
| 60 |
%define __?ALIGN_32BIT_4B?__ 0x66,0x66,0x66,0x90 |
| 61 |
%define __?ALIGN_32BIT_GROUP?__ 4 |
| 62 |
|
| 63 |
%define __?ALIGN_64BIT_1B?__ 0x90 |
| 64 |
%define __?ALIGN_64BIT_2B?__ 0x66,0x90 |
| 65 |
%define __?ALIGN_64BIT_3B?__ 0x66,0x66,0x90 |
| 66 |
%define __?ALIGN_64BIT_4B?__ 0x66,0x66,0x66,0x90 |
| 67 |
%define __?ALIGN_64BIT_GROUP?__ 4 |
| 68 |
%elifidni %1,k7 |
| 69 |
%define __?ALIGN_JMP_THRESHOLD?__ 16 |
| 70 |
|
| 71 |
%define __?ALIGN_16BIT_1B?__ 0x90 |
| 72 |
%define __?ALIGN_16BIT_2B?__ 0x66,0x90 |
| 73 |
%define __?ALIGN_16BIT_3B?__ 0x66,0x66,0x90 |
| 74 |
%define __?ALIGN_16BIT_4B?__ 0x66,0x66,0x66,0x90 |
| 75 |
%define __?ALIGN_64BIT_GROUP?__ 4 |
| 76 |
|
| 77 |
%define __?ALIGN_32BIT_1B?__ 0x90 |
| 78 |
%define __?ALIGN_32BIT_2B?__ 0x8b,0xc0 |
| 79 |
%define __?ALIGN_32BIT_3B?__ 0x8d,0x04,0x20 |
| 80 |
%define __?ALIGN_32BIT_4B?__ 0x8d,0x44,0x20,0x00 |
| 81 |
%define __?ALIGN_32BIT_5B?__ 0x8d,0x44,0x20,0x00,0x90 |
| 82 |
%define __?ALIGN_32BIT_6B?__ 0x8d,0x80,0x00,0x00,0x00,0x00 |
| 83 |
%define __?ALIGN_32BIT_7B?__ 0x8d,0x04,0x05,0x00,0x00,0x00,0x00 |
| 84 |
%define __?ALIGN_32BIT_GROUP?__ 7 |
| 85 |
|
| 86 |
%define __?ALIGN_64BIT_1B?__ 0x90 |
| 87 |
%define __?ALIGN_64BIT_2B?__ 0x66,0x90 |
| 88 |
%define __?ALIGN_64BIT_3B?__ 0x66,0x66,0x90 |
| 89 |
%define __?ALIGN_64BIT_4B?__ 0x66,0x66,0x66,0x90 |
| 90 |
%define __?ALIGN_64BIT_GROUP?__ 4 |
| 91 |
%elifidni %1,p6 |
| 92 |
%define __?ALIGN_JMP_THRESHOLD?__ 16 |
| 93 |
|
| 94 |
%define __?ALIGN_16BIT_1B?__ 0x90 |
| 95 |
%define __?ALIGN_16BIT_2B?__ 0x66,0x90 |
| 96 |
%define __?ALIGN_16BIT_3B?__ 0x0f,0x1f,0x00 |
| 97 |
%define __?ALIGN_16BIT_4B?__ 0x0f,0x1f,0x40,0x00 |
| 98 |
%define __?ALIGN_16BIT_GROUP?__ 4 |
| 99 |
|
| 100 |
%define __?ALIGN_32BIT_1B?__ 0x90 |
| 101 |
%define __?ALIGN_32BIT_2B?__ 0x66,0x90 |
| 102 |
%define __?ALIGN_32BIT_3B?__ 0x0f,0x1f,0x00 |
| 103 |
%define __?ALIGN_32BIT_4B?__ 0x0f,0x1f,0x40,0x00 |
| 104 |
%define __?ALIGN_32BIT_5B?__ 0x0f,0x1f,0x44,0x00,0x00 |
| 105 |
%define __?ALIGN_32BIT_6B?__ 0x66,0x0f,0x1f,0x44,0x00,0x00 |
| 106 |
%define __?ALIGN_32BIT_7B?__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00 |
| 107 |
%define __?ALIGN_32BIT_8B?__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00 |
| 108 |
%define __?ALIGN_32BIT_GROUP?__ 8 |
| 109 |
|
| 110 |
%define __?ALIGN_64BIT_1B?__ 0x90 |
| 111 |
%define __?ALIGN_64BIT_2B?__ 0x66,0x90 |
| 112 |
%define __?ALIGN_64BIT_3B?__ 0x0f,0x1f,0x00 |
| 113 |
%define __?ALIGN_64BIT_4B?__ 0x0f,0x1f,0x40,0x00 |
| 114 |
%define __?ALIGN_64BIT_5B?__ 0x0f,0x1f,0x44,0x00,0x00 |
| 115 |
%define __?ALIGN_64BIT_6B?__ 0x66,0x0f,0x1f,0x44,0x00,0x00 |
| 116 |
%define __?ALIGN_64BIT_7B?__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00 |
| 117 |
%define __?ALIGN_64BIT_8B?__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00 |
| 118 |
%define __?ALIGN_64BIT_GROUP?__ 8 |
| 119 |
%else |
| 120 |
%error unknown alignment mode: %1 |
| 121 |
%endif |
| 122 |
%ifnempty %2 |
| 123 |
%ifidni %2,nojmp |
| 124 |
%xdefine __?ALIGN_JMP_THRESHOLD?__ -1 |
| 125 |
%else |
| 126 |
%xdefine __?ALIGN_JMP_THRESHOLD?__ %2 |
| 127 |
%endif |
| 128 |
%endif |
| 129 |
%xdefine __?ALIGNMODE?__ %1,__?ALIGN_JMP_THRESHOLD?__ |
| 130 |
%endmacro |
| 131 |
|
| 132 |
%defalias __ALIGNMODE__ __?ALIGNMODE?__ |
| 133 |
|
| 134 |
%unimacro align 1-2+.nolist |
| 135 |
%imacro align 1-2+.nolist |
| 136 |
sectalign %1 ; align a segment as well |
| 137 |
%ifnempty %2 |
| 138 |
times (((%1) - (($-$$) % (%1))) % (%1)) %2 |
| 139 |
%elif __?PASS?__ == 0 || __?PASS?__ == 3 |
| 140 |
times (((%1) - (($-$$) % (%1))) % (%1)) nop |
| 141 |
%else |
| 142 |
%push |
| 143 |
%assign %$pad (((%1) - (($-$$) % (%1))) % (%1)) |
| 144 |
%if __?ALIGN_JMP_THRESHOLD?__ != -1 && %$pad > __?ALIGN_JMP_THRESHOLD?__ |
| 145 |
jmp %$end |
| 146 |
; We can't re-use %$pad here as $ will have changed! |
| 147 |
times (((%1) - (($-$$) % (%1))) % (%1)) nop |
| 148 |
%else |
| 149 |
times (%$pad / __?ALIGN_%[__?BITS?__]BIT_GROUP?__) \ |
| 150 |
db __?ALIGN_%[__?BITS?__]BIT_%[__?ALIGN_%[__?BITS?__]BIT_GROUP?__]B?__ |
| 151 |
%assign %$pad %$pad % __?ALIGN_%[__?BITS?__]BIT_GROUP?__ |
| 152 |
%if %$pad > 0 |
| 153 |
db __?ALIGN_%[__?BITS?__]BIT_%[%$pad]B?__ |
| 154 |
%endif |
| 155 |
%endif |
| 156 |
%$end: |
| 157 |
%pop |
| 158 |
%endif |
| 159 |
%endmacro |
| 160 |
|
| 161 |
alignmode generic |