1 ;; --------------------------------------------------------------------------
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.
7 ;; Redistribution and use in source and binary forms, with or without
8 ;; modification, are permitted provided that the following
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.
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.
32 ;; --------------------------------------------------------------------------
35 ; Smart alignment macros
39 %imacro alignmode 1-2.nolist
41 %define __?ALIGN_JMP_THRESHOLD?__ 16
43 %define __?ALIGN_16BIT_1B?__ 0x90
44 %define __?ALIGN_16BIT_GROUP?__ 1
46 %define __?ALIGN_32BIT_1B?__ 0x90
47 %define __?ALIGN_32BIT_GROUP?__ 1
49 %define __?ALIGN_64BIT_1B?__ 0x90
50 %define __?ALIGN_64BIT_GROUP?__ 1
52 %define __?ALIGN_JMP_THRESHOLD?__ 8
54 %define __?ALIGN_16BIT_1B?__ 0x90
55 %define __?ALIGN_16BIT_2B?__ 0x89,0xf6
56 %define __?ALIGN_16BIT_3B?__ 0x8d,0x74,0x00
57 %define __?ALIGN_16BIT_4B?__ 0x8d,0xb4,0x00,0x00
58 %define __?ALIGN_16BIT_5B?__ 0x8d,0xb4,0x00,0x00,0x90
59 %define __?ALIGN_16BIT_6B?__ 0x8d,0xb4,0x00,0x00,0x89,0xff
60 %define __?ALIGN_16BIT_7B?__ 0x8d,0xb4,0x00,0x00,0x8d,0x7d,0x00
61 %define __?ALIGN_16BIT_8B?__ 0x8d,0xb4,0x00,0x00,0x8d,0xbd,0x00,0x00
62 %define __?ALIGN_16BIT_GROUP?__ 8
64 %define __?ALIGN_32BIT_1B?__ 0x90
65 %define __?ALIGN_32BIT_2B?__ 0x89,0xf6
66 %define __?ALIGN_32BIT_3B?__ 0x8d,0x76,0x00
67 %define __?ALIGN_32BIT_4B?__ 0x8d,0x74,0x26,0x00
68 %define __?ALIGN_32BIT_5B?__ 0x90,0x8d,0x74,0x26,0x00
69 %define __?ALIGN_32BIT_6B?__ 0x8d,0xb6,0x00,0x00,0x00,0x00
70 %define __?ALIGN_32BIT_7B?__ 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
71 %define __?ALIGN_32BIT_GROUP?__ 7
73 %define __?ALIGN_64BIT_1B?__ 0x90
74 %define __?ALIGN_64BIT_2B?__ 0x66,0x90
75 %define __?ALIGN_64BIT_3B?__ 0x66,0x66,0x90
76 %define __?ALIGN_64BIT_4B?__ 0x66,0x66,0x66,0x90
77 %define __?ALIGN_64BIT_GROUP?__ 4
79 %define __?ALIGN_JMP_THRESHOLD?__ 16
81 %define __?ALIGN_16BIT_1B?__ 0x90
82 %define __?ALIGN_16BIT_2B?__ 0x66,0x90
83 %define __?ALIGN_16BIT_3B?__ 0x66,0x66,0x90
84 %define __?ALIGN_16BIT_4B?__ 0x66,0x66,0x66,0x90
85 %define __?ALIGN_16BIT_GROUP?__ 4
87 %define __?ALIGN_32BIT_1B?__ 0x90
88 %define __?ALIGN_32BIT_2B?__ 0x66,0x90
89 %define __?ALIGN_32BIT_3B?__ 0x66,0x66,0x90
90 %define __?ALIGN_32BIT_4B?__ 0x66,0x66,0x66,0x90
91 %define __?ALIGN_32BIT_GROUP?__ 4
93 %define __?ALIGN_64BIT_1B?__ 0x90
94 %define __?ALIGN_64BIT_2B?__ 0x66,0x90
95 %define __?ALIGN_64BIT_3B?__ 0x66,0x66,0x90
96 %define __?ALIGN_64BIT_4B?__ 0x66,0x66,0x66,0x90
97 %define __?ALIGN_64BIT_GROUP?__ 4
99 %define __?ALIGN_JMP_THRESHOLD?__ 16
101 %define __?ALIGN_16BIT_1B?__ 0x90
102 %define __?ALIGN_16BIT_2B?__ 0x66,0x90
103 %define __?ALIGN_16BIT_3B?__ 0x66,0x66,0x90
104 %define __?ALIGN_16BIT_4B?__ 0x66,0x66,0x66,0x90
105 %define __?ALIGN_64BIT_GROUP?__ 4
107 %define __?ALIGN_32BIT_1B?__ 0x90
108 %define __?ALIGN_32BIT_2B?__ 0x8b,0xc0
109 %define __?ALIGN_32BIT_3B?__ 0x8d,0x04,0x20
110 %define __?ALIGN_32BIT_4B?__ 0x8d,0x44,0x20,0x00
111 %define __?ALIGN_32BIT_5B?__ 0x8d,0x44,0x20,0x00,0x90
112 %define __?ALIGN_32BIT_6B?__ 0x8d,0x80,0x00,0x00,0x00,0x00
113 %define __?ALIGN_32BIT_7B?__ 0x8d,0x04,0x05,0x00,0x00,0x00,0x00
114 %define __?ALIGN_32BIT_GROUP?__ 7
116 %define __?ALIGN_64BIT_1B?__ 0x90
117 %define __?ALIGN_64BIT_2B?__ 0x66,0x90
118 %define __?ALIGN_64BIT_3B?__ 0x66,0x66,0x90
119 %define __?ALIGN_64BIT_4B?__ 0x66,0x66,0x66,0x90
120 %define __?ALIGN_64BIT_GROUP?__ 4
122 %define __?ALIGN_JMP_THRESHOLD?__ 16
124 %define __?ALIGN_16BIT_1B?__ 0x90
125 %define __?ALIGN_16BIT_2B?__ 0x66,0x90
126 %define __?ALIGN_16BIT_3B?__ 0x0f,0x1f,0x00
127 %define __?ALIGN_16BIT_4B?__ 0x0f,0x1f,0x40,0x00
128 %define __?ALIGN_16BIT_GROUP?__ 4
130 %define __?ALIGN_32BIT_1B?__ 0x90
131 %define __?ALIGN_32BIT_2B?__ 0x66,0x90
132 %define __?ALIGN_32BIT_3B?__ 0x0f,0x1f,0x00
133 %define __?ALIGN_32BIT_4B?__ 0x0f,0x1f,0x40,0x00
134 %define __?ALIGN_32BIT_5B?__ 0x0f,0x1f,0x44,0x00,0x00
135 %define __?ALIGN_32BIT_6B?__ 0x66,0x0f,0x1f,0x44,0x00,0x00
136 %define __?ALIGN_32BIT_7B?__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
137 %define __?ALIGN_32BIT_8B?__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
138 %define __?ALIGN_32BIT_GROUP?__ 8
140 %define __?ALIGN_64BIT_1B?__ 0x90
141 %define __?ALIGN_64BIT_2B?__ 0x66,0x90
142 %define __?ALIGN_64BIT_3B?__ 0x0f,0x1f,0x00
143 %define __?ALIGN_64BIT_4B?__ 0x0f,0x1f,0x40,0x00
144 %define __?ALIGN_64BIT_5B?__ 0x0f,0x1f,0x44,0x00,0x00
145 %define __?ALIGN_64BIT_6B?__ 0x66,0x0f,0x1f,0x44,0x00,0x00
146 %define __?ALIGN_64BIT_7B?__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
147 %define __?ALIGN_64BIT_8B?__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
148 %define __?ALIGN_64BIT_GROUP?__ 8
150 %error unknown alignment mode: %1
154 %xdefine __?ALIGN_JMP_THRESHOLD?__ -1
156 %xdefine __?ALIGN_JMP_THRESHOLD?__ %2
159 %xdefine __?ALIGNMODE?__ %1,__?ALIGN_JMP_THRESHOLD?__
162 %defalias __ALIGNMODE__ __?ALIGNMODE?__
164 %unimacro align 1-2+.nolist
165 %imacro align 1-2+.nolist
166 sectalign %1 ; align a segment as well
168 times (((%1) - (($-$$) % (%1))) % (%1)) %2
169 %elif __?PASS?__ == 0 || __?PASS?__ == 3
170 times (((%1) - (($-$$) % (%1))) % (%1)) nop
173 %assign %$pad (((%1) - (($-$$) % (%1))) % (%1))
174 %if __?ALIGN_JMP_THRESHOLD?__ != -1 && %$pad > __?ALIGN_JMP_THRESHOLD?__
176 ; We can't re-use %$pad here as $ will have changed!
177 times (((%1) - (($-$$) % (%1))) % (%1)) nop
179 times (%$pad / __?ALIGN_%[__?BITS?__]BIT_GROUP?__) \
180 db __?ALIGN_%[__?BITS?__]BIT_%[__?ALIGN_%[__?BITS?__]BIT_GROUP?__]B?__
181 %assign %$pad %$pad % __?ALIGN_%[__?BITS?__]BIT_GROUP?__
183 db __?ALIGN_%[__?BITS?__]BIT_%[%$pad]B?__