smartalign.mac: smart alignments macro package
[nasm/perl-rewrite.git] / macros / smartalign.mac
blob1a631edcf137e844d0448e61f7e6ef5cc053b719
2 ; Smart alignment macros
4 USE: smartalign
6 %imacro alignmode 1-2.nolist
7   %ifnempty %2
8     %xdefine __ALIGN_JMP_THRESHOLD__ %2
9   %else
10     %define __ALIGN_JMP_THRESHOLD__ 17
11   %endif
12   %ifidni %1,nop
13     %define __ALIGN_16BIT_1B__ 0x90
14     %define __ALIGN_16BIT_2B__ 0x90,0x90
15     %define __ALIGN_16BIT_3B__ 0x90,0x90,0x90
16     %define __ALIGN_16BIT_4B__ 0x90,0x90,0x90,0x90
17     %define __ALIGN_16BIT_5B__ 0x90,0x90,0x90,0x90,0x90
18     %define __ALIGN_16BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
19     %define __ALIGN_16BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
20     %define __ALIGN_16BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
22     %define __ALIGN_32BIT_1B__ 0x90
23     %define __ALIGN_32BIT_2B__ 0x90,0x90
24     %define __ALIGN_32BIT_3B__ 0x90,0x90,0x90
25     %define __ALIGN_32BIT_4B__ 0x90,0x90,0x90,0x90
26     %define __ALIGN_32BIT_5B__ 0x90,0x90,0x90,0x90,0x90
27     %define __ALIGN_32BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
28     %define __ALIGN_32BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
29     %define __ALIGN_32BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
31     %define __ALIGN_64BIT_1B__ 0x90
32     %define __ALIGN_64BIT_2B__ 0x90,0x90
33     %define __ALIGN_64BIT_3B__ 0x90,0x90,0x90
34     %define __ALIGN_64BIT_4B__ 0x90,0x90,0x90,0x90
35     %define __ALIGN_64BIT_5B__ 0x90,0x90,0x90,0x90,0x90
36     %define __ALIGN_64BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
37     %define __ALIGN_64BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
38     %define __ALIGN_64BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
39   %elifidni %1,generic
40     %define __ALIGN_16BIT_1B__ 0x90
41     %define __ALIGN_16BIT_2B__ 0x90,0x90
42     %define __ALIGN_16BIT_3B__ 0x90,0x90,0x90
43     %define __ALIGN_16BIT_4B__ 0x90,0x90,0x90,0x90
44     %define __ALIGN_16BIT_5B__ 0x90,0x90,0x90,0x90,0x90
45     %define __ALIGN_16BIT_6B__ 0x90,0x90,0x90,0x90,0x90,0x90
46     %define __ALIGN_16BIT_7B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90
47     %define __ALIGN_16BIT_8B__ 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90
49     %define __ALIGN_32BIT_1B__ 0x90
50     %define __ALIGN_32BIT_2B__ 0x89,0xf6
51     %define __ALIGN_32BIT_3B__ 0x8d,0x76,0x00
52     %define __ALIGN_32BIT_4B__ 0x8d,0x74,0x26,0x00
53     %define __ALIGN_32BIT_5B__ 0x90,0x8d,0x74,0x26,0x00
54     %define __ALIGN_32BIT_6B__ 0x8d,0xb6,0x00,0x00,0x00,0x00
55     %define __ALIGN_32BIT_7B__ 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
56     %undef  __ALIGN_32BIT_8B__
58     %define __ALIGN_64BIT_1B__ 0x90
59     %define __ALIGN_64BIT_2B__ 0x66,0x90
60     %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
61     %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
62     %define __ALIGN_64BIT_5B__ 0x66,0x66,0x90,0x66,0x90
63     %define __ALIGN_64BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
64     %define __ALIGN_64BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
65     %define __ALIGN_64BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
66   %elifidni %1,k8
67     %define __ALIGN_16BIT_1B__ 0x90
68     %define __ALIGN_16BIT_2B__ 0x66,0x90
69     %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
70     %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
71     %define __ALIGN_16BIT_5B__ 0x66,0x66,0x90,0x66,0x90
72     %define __ALIGN_16BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
73     %define __ALIGN_16BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
74     %define __ALIGN_16BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
76     %define __ALIGN_32BIT_1B__ 0x90
77     %define __ALIGN_32BIT_2B__ 0x66,0x90
78     %define __ALIGN_32BIT_3B__ 0x66,0x66,0x90
79     %define __ALIGN_32BIT_4B__ 0x66,0x66,0x66,0x90
80     %define __ALIGN_32BIT_5B__ 0x66,0x66,0x90,0x66,0x90
81     %define __ALIGN_32BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
82     %define __ALIGN_32BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
83     %define __ALIGN_32BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
85     %define __ALIGN_64BIT_1B__ 0x90
86     %define __ALIGN_64BIT_2B__ 0x66,0x90
87     %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
88     %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
89     %define __ALIGN_64BIT_5B__ 0x66,0x66,0x90,0x66,0x90
90     %define __ALIGN_64BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
91     %define __ALIGN_64BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
92     %define __ALIGN_64BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
93   %elifidni %1,k7
94     %define __ALIGN_16BIT_1B__ 0x90
95     %define __ALIGN_16BIT_2B__ 0x66,0x90
96     %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
97     %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
98     %define __ALIGN_16BIT_5B__ 0x66,0x66,0x90,0x66,0x90
99     %define __ALIGN_16BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
100     %define __ALIGN_16BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
101     %define __ALIGN_16BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
103     %define __ALIGN_32BIT_1B__ 0x90
104     %define __ALIGN_32BIT_2B__ 0x8b,0xc0
105     %define __ALIGN_32BIT_3B__ 0x8d,0x04,0x20
106     %define __ALIGN_32BIT_4B__ 0x8d,0x44,0x20,0x00
107     %define __ALIGN_32BIT_5B__ 0x8d,0x44,0x20,0x00,0x90
108     %define __ALIGN_32BIT_6B__ 0x8d,0x80,0x00,0x00,0x00,0x00
109     %define __ALIGN_32BIT_7B__ 0x8d,0x04,0x05,0x00,0x00,0x00,0x00
110     %undef  __ALIGN_32BIT_8B__
112     %define __ALIGN_64BIT_1B__ 0x90
113     %define __ALIGN_64BIT_2B__ 0x66,0x90
114     %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
115     %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
116     %define __ALIGN_64BIT_5B__ 0x66,0x66,0x90,0x66,0x90
117     %define __ALIGN_64BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
118     %define __ALIGN_64BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
119     %define __ALIGN_64BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
120   %elifidni %1,p6
121     %define __ALIGN_16BIT_1B__ 0x90
122     %define __ALIGN_16BIT_2B__ 0x66,0x90
123     %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
124     %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
125     %define __ALIGN_16BIT_5B__ 0x66,0x66,0x90,0x66,0x90
126     %define __ALIGN_16BIT_6B__ 0x66,0x66,0x90,0x66,0x66,0x90
127     %define __ALIGN_16BIT_7B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x90
128     %define __ALIGN_16BIT_8B__ 0x66,0x66,0x66,0x90,0x66,0x66,0x66,0x90
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
139     %define __ALIGN_64BIT_1B__ 0x90
140     %define __ALIGN_64BIT_2B__ 0x66,0x90
141     %define __ALIGN_64BIT_3B__ 0x0f,0x1f,0x00
142     %define __ALIGN_64BIT_4B__ 0x0f,0x1f,0x40,0x00
143     %define __ALIGN_64BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
144     %define __ALIGN_64BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
145     %define __ALIGN_64BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
146     %define __ALIGN_64BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
147   %else
148     %error unknown alignment mode: %1
149   %endif
150   %xdefine __ALIGNMODE__ %1,__ALIGN_JMP_THRESHOLD__
151 %endmacro
153 %unimacro align 1-2+.nolist
154 %imacro align 1-2+.nolist
155   %ifnempty %2
156     times ($$-$) % (%1) %2
157   %else
158     %push
159     %assign %$pad ($$-$) % %1
160     %if %$pad >= __ALIGN_JMP_THRESHOLD__
161       jmp %%end
162       ; We can't re-use %$pad here as $ will have changed!
163       times ($$-$) % %1 db 90h
164 %%end:
165     %else
166       %if __BITS__ == 16
167         %ifdef __ALIGN_16BIT_8B__
168           %rep %$pad / 8
169             db __ALIGN_16BIT_8B__
170           %endrep
171           %assign %$pad %$pad % 8
172         %else
173           %rep %$pad / 7
174             db __ALIGN_16BIT_7B__
175           %endrep
176           %assign %$pad %$pad % 7
177         %endif
178         %if %$pad == 1
179           db __ALIGN_16BIT_1B__
180         %elif %$pad == 2
181           db __ALIGN_16BIT_2B__
182         %elif %$pad == 3
183           db __ALIGN_16BIT_3B__
184         %elif %$pad == 4
185           db __ALIGN_16BIT_4B__
186         %elif %$pad == 5
187           db __ALIGN_16BIT_5B__
188         %elif %$pad == 6
189           db __ALIGN_16BIT_6B__
190         %elif %$pad == 7
191           db __ALIGN_16BIT_7B__
192         %endif
193       %elif __BITS__ == 32
194         %ifdef __ALIGN_32BIT_8B__
195           %rep %$pad / 8
196             db __ALIGN_32BIT_8B__
197           %endrep
198           %assign %$pad %$pad % 8
199         %else
200           %rep %$pad / 7
201             db __ALIGN_32BIT_7B__
202           %endrep
203           %assign %$pad %$pad % 7
204         %endif
205         %if %$pad == 1
206           db __ALIGN_32BIT_1B__
207         %elif %$pad == 2
208           db __ALIGN_32BIT_2B__
209         %elif %$pad == 3
210           db __ALIGN_32BIT_3B__
211         %elif %$pad == 4
212           db __ALIGN_32BIT_4B__
213         %elif %$pad == 5
214           db __ALIGN_32BIT_5B__
215         %elif %$pad == 6
216           db __ALIGN_32BIT_6B__
217         %elif %$pad == 7
218           db __ALIGN_32BIT_7B__
219         %endif
220       %elif __BITS__ == 64
221         %ifdef __ALIGN_64BIT_8B__
222           %rep %$pad / 8
223             db __ALIGN_64BIT_8B__
224           %endrep
225           %assign %$pad %$pad % 8
226         %else
227           %rep %$pad / 7
228             db __ALIGN_64BIT_7B__
229           %endrep
230           %assign %$pad %$pad % 7
231         %endif
232         %if %$pad == 1
233           db __ALIGN_64BIT_1B__
234         %elif %$pad == 2
235           db __ALIGN_64BIT_2B__
236         %elif %$pad == 3
237           db __ALIGN_64BIT_3B__
238         %elif %$pad == 4
239           db __ALIGN_64BIT_4B__
240         %elif %$pad == 5
241           db __ALIGN_64BIT_5B__
242         %elif %$pad == 6
243           db __ALIGN_64BIT_6B__
244         %elif %$pad == 7
245           db __ALIGN_64BIT_7B__
246         %endif
247       %else
248         %error "Invalid __BITS__ value"
249       %endif
250     %endif
251     %pop
252   %endif
253 %endmacro
255         alignmode generic