doc: document the %[...] construct.
[nasm.git] / macros / smartalign.mac
blob48c07de51923ad20295dabe4688de62483cd2c14
2 ; Smart alignment macros
4 USE: smartalign
6 %imacro alignmode 1-2.nolist
7   %ifidni %1,nop
8     %define __ALIGN_JMP_THRESHOLD__ 16
10     %define __ALIGN_16BIT_1B__ 0x90
11     %define __ALIGN_16BIT_2B__ 0x90,0x90
12     %define __ALIGN_16BIT_3B__ 0x90,0x90,0x90
13     %define __ALIGN_16BIT_4B__ 0x90,0x90,0x90,0x90
14     %define __ALIGN_16BIT_5B__ 0x90,0x90,0x90,0x90,0x90
15     %define __ALIGN_16BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
16     %define __ALIGN_16BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
17     %define __ALIGN_16BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
18     %define __ALIGN_16BIT_GROUP__ 8
20     %define __ALIGN_32BIT_1B__ 0x90
21     %define __ALIGN_32BIT_2B__ 0x90,0x90
22     %define __ALIGN_32BIT_3B__ 0x90,0x90,0x90
23     %define __ALIGN_32BIT_4B__ 0x90,0x90,0x90,0x90
24     %define __ALIGN_32BIT_5B__ 0x90,0x90,0x90,0x90,0x90
25     %define __ALIGN_32BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
26     %define __ALIGN_32BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
27     %define __ALIGN_32BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
28     %define __ALIGN_32BIT_GROUP__ 8
30     %define __ALIGN_64BIT_1B__ 0x90
31     %define __ALIGN_64BIT_2B__ 0x90,0x90
32     %define __ALIGN_64BIT_3B__ 0x90,0x90,0x90
33     %define __ALIGN_64BIT_4B__ 0x90,0x90,0x90,0x90
34     %define __ALIGN_64BIT_5B__ 0x90,0x90,0x90,0x90,0x90
35     %define __ALIGN_64BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
36     %define __ALIGN_64BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
37     %define __ALIGN_64BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
38     %define __ALIGN_64BIT_GROUP__ 8
39   %elifidni %1,generic
40     %define __ALIGN_JMP_THRESHOLD__ 8
42     %define __ALIGN_16BIT_1B__ 0x90
43     %define __ALIGN_16BIT_2B__ 0x89,0xf6
44     %define __ALIGN_16BIT_3B__ 0x8d,0x74,0x00
45     %define __ALIGN_16BIT_4B__ 0x8d,0xb4,0x00,0x00
46     %define __ALIGN_16BIT_5B__ 0x90,0x8d,0xb4,0x00,0x00
47     %define __ALIGN_16BIT_6B__ 0x89,0xf6,0x8d,0xbd,0x00,0x00
48     %define __ALIGN_16BIT_7B__ 0x8d,0x74,0x00,0x8d,0xbd,0x00,0x00
49     %define __ALIGN_16BIT_8B__ 0x8d,0xb4,0x00,0x00,0x8d,0xbd,0x00,0x00
50     %define __ALIGN_16BIT_GROUP__ 8
52     %define __ALIGN_32BIT_1B__ 0x90
53     %define __ALIGN_32BIT_2B__ 0x89,0xf6
54     %define __ALIGN_32BIT_3B__ 0x8d,0x76,0x00
55     %define __ALIGN_32BIT_4B__ 0x8d,0x74,0x26,0x00
56     %define __ALIGN_32BIT_5B__ 0x90,0x8d,0x74,0x26,0x00
57     %define __ALIGN_32BIT_6B__ 0x8d,0xb6,0x00,0x00,0x00,0x00
58     %define __ALIGN_32BIT_7B__ 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
59     %undef  __ALIGN_32BIT_8B__
60     %define __ALIGN_32BIT_GROUP__ 7
62     %define __ALIGN_64BIT_1B__ 0x90
63     %define __ALIGN_64BIT_2B__ 0x66,0x90
64     %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
65     %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
66     %define __ALIGN_64BIT_5B__ 0x66,0x66,0x90,0x66,0x90
67     %define __ALIGN_64BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
68     %define __ALIGN_64BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
69     %define __ALIGN_64BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
70     %define __ALIGN_64BIT_GROUP__ 8
71   %elifidni %1,k8
72     %define __ALIGN_JMP_THRESHOLD__ 16
74     %define __ALIGN_16BIT_1B__ 0x90
75     %define __ALIGN_16BIT_2B__ 0x66,0x90
76     %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
77     %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
78     %define __ALIGN_16BIT_5B__ 0x66,0x66,0x90,0x66,0x90
79     %define __ALIGN_16BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
80     %define __ALIGN_16BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
81     %define __ALIGN_16BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
82     %define __ALIGN_16BIT_GROUP__ 8
84     %define __ALIGN_32BIT_1B__ 0x90
85     %define __ALIGN_32BIT_2B__ 0x66,0x90
86     %define __ALIGN_32BIT_3B__ 0x66,0x66,0x90
87     %define __ALIGN_32BIT_4B__ 0x66,0x66,0x66,0x90
88     %define __ALIGN_32BIT_5B__ 0x66,0x66,0x90,0x66,0x90
89     %define __ALIGN_32BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
90     %define __ALIGN_32BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
91     %define __ALIGN_32BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
92     %define __ALIGN_32BIT_GROUP__ 8
94     %define __ALIGN_64BIT_1B__ 0x90
95     %define __ALIGN_64BIT_2B__ 0x66,0x90
96     %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
97     %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
98     %define __ALIGN_64BIT_5B__ 0x66,0x66,0x90,0x66,0x90
99     %define __ALIGN_64BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
100     %define __ALIGN_64BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
101     %define __ALIGN_64BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
102     %define __ALIGN_64BIT_GROUP__ 8
103   %elifidni %1,k7
104     %define __ALIGN_JMP_THRESHOLD__ 16
106     %define __ALIGN_16BIT_1B__ 0x90
107     %define __ALIGN_16BIT_2B__ 0x66,0x90
108     %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
109     %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
110     %define __ALIGN_16BIT_5B__ 0x66,0x66,0x90,0x66,0x90
111     %define __ALIGN_16BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
112     %define __ALIGN_16BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
113     %define __ALIGN_16BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
114     %define __ALIGN_64BIT_GROUP__ 8
116     %define __ALIGN_32BIT_1B__ 0x90
117     %define __ALIGN_32BIT_2B__ 0x8b,0xc0
118     %define __ALIGN_32BIT_3B__ 0x8d,0x04,0x20
119     %define __ALIGN_32BIT_4B__ 0x8d,0x44,0x20,0x00
120     %define __ALIGN_32BIT_5B__ 0x8d,0x44,0x20,0x00,0x90
121     %define __ALIGN_32BIT_6B__ 0x8d,0x80,0x00,0x00,0x00,0x00
122     %define __ALIGN_32BIT_7B__ 0x8d,0x04,0x05,0x00,0x00,0x00,0x00
123     %undef  __ALIGN_32BIT_8B__
124     %define __ALIGN_32BIT_GROUP__ 7
126     %define __ALIGN_64BIT_1B__ 0x90
127     %define __ALIGN_64BIT_2B__ 0x66,0x90
128     %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
129     %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
130     %define __ALIGN_64BIT_5B__ 0x66,0x66,0x90,0x66,0x90
131     %define __ALIGN_64BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
132     %define __ALIGN_64BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
133     %define __ALIGN_64BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
134     %define __ALIGN_64BIT_GROUP__ 8
135   %elifidni %1,p6
136     %define __ALIGN_JMP_THRESHOLD__ 16
138     %define __ALIGN_16BIT_1B__ 0x90
139     %define __ALIGN_16BIT_2B__ 0x66,0x90
140     %define __ALIGN_16BIT_3B__ 0x0f,0x1f,0x00
141     %define __ALIGN_16BIT_4B__ 0x0f,0x1f,0x40,0x00
142     %define __ALIGN_16BIT_5B__ 0x90,0x0f,0x1f,0x40,0x00
143     %define __ALIGN_16BIT_6B__ 0x0f,0x1f,0x00,0x0f,0x1f,0x00
144     %define __ALIGN_16BIT_7B__ 0x0f,0x1f,0x00,0x0f,0x1f,0x40,0x00
145     %define __ALIGN_16BIT_8B__ 0x0f,0x1f,0x40,0x00,0x0f,0x1f,0x40,0x00
146     %define __ALIGN_16BIT_GROUP__ 8
148     %define __ALIGN_32BIT_1B__ 0x90
149     %define __ALIGN_32BIT_2B__ 0x66,0x90
150     %define __ALIGN_32BIT_3B__ 0x0f,0x1f,0x00
151     %define __ALIGN_32BIT_4B__ 0x0f,0x1f,0x40,0x00
152     %define __ALIGN_32BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
153     %define __ALIGN_32BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
154     %define __ALIGN_32BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
155     %define __ALIGN_32BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
156     %define __ALIGN_32BIT_GROUP__ 8
158     %define __ALIGN_64BIT_1B__ 0x90
159     %define __ALIGN_64BIT_2B__ 0x66,0x90
160     %define __ALIGN_64BIT_3B__ 0x0f,0x1f,0x00
161     %define __ALIGN_64BIT_4B__ 0x0f,0x1f,0x40,0x00
162     %define __ALIGN_64BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
163     %define __ALIGN_64BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
164     %define __ALIGN_64BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
165     %define __ALIGN_64BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
166     %define __ALIGN_64BIT_GROUP__ 8
167   %else
168     %error unknown alignment mode: %1
169   %endif
170   %ifnempty %2
171     %xdefine __ALIGN_JMP_THRESHOLD__ %2
172   %endif
173   %xdefine __ALIGNMODE__ %1,__ALIGN_JMP_THRESHOLD__
174 %endmacro
176 %unimacro align 1-2+.nolist
177 %imacro align 1-2+.nolist
178   %ifnempty %2
179     times (((%1) - (($-$$) % (%1))) % (%1)) %2
180   %else
181     %push
182     %assign %$pad (((%1) - (($-$$) % (%1))) % (%1))
183     %if %$pad > __ALIGN_JMP_THRESHOLD__
184       jmp %$end
185       ; We can't re-use %$pad here as $ will have changed!
186       times (((%1) - (($-$$) % (%1))) % (%1)) nop
187 %$end:
188     %else
189       times (%$pad / __ALIGN_%[__BITS__]BIT_GROUP__) \
190         db __ALIGN_%[__BITS__]BIT_%[__ALIGN_%[__BITS__]BIT_GROUP__]B__
191       %assign %$pad %$pad % __ALIGN_%[__BITS__]BIT_GROUP__
192       %if %$pad > 0
193         db __ALIGN_%[__BITS__]BIT_%[%$pad]B__
194       %endif
195     %endif
196     %pop
197   %endif
198 %endmacro
200         alignmode generic