* Sync with recent trunk (r52637).
[reactos.git] / include / asm / asm.inc
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel
4 * FILE: ntoskrnl/include/amd64/asmmacro.S
5 * PURPOSE: ASM macros for for GAS and MASM/ML64
6 * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
7 */
8
9 #ifdef _USE_ML
10
11 /* Allow ".name" identifiers */
12 OPTION DOTNAME
13
14 #ifdef _M_IX86
15 .686P
16 .XMM
17 .MODEL FLAT
18 ASSUME CS:NOTHING, DS:NOTHING, ES:NOTHING, FS:NOTHING, GS:NOTHING
19 #endif
20
21 /* Hex numbers need to be in 01ABh format */
22 #define HEX(x) 0##x##h
23
24 /* Macro values need to be marked */
25 #define VAL(x) x
26
27 /* MASM/ML doesn't want explicit [rip] addressing */
28 rip = 0
29
30 /* Due to MASM's reverse syntax, we are forced to use a precompiler macro */
31 #define MACRO(name, ...) name MACRO __VA_ARGS__
32
33 /* To avoid reverse syntax we provide a new macro .PROC, replacing PROC... */
34 .PROC MACRO name
35 _&name PROC
36 ENDM
37 #define FUNC .PROC
38
39 /* ... and .ENDP, replacing ENDP */
40 .ENDP MACRO name
41 _&name ENDP
42 ENDM
43 #define ENDFUNC .ENDP
44
45 /* check http://msdn.microsoft.com/en-us/library/9c9k076y%28VS.80%29.aspx
46 and http://msdn.microsoft.com/en-us/library/ms679352%28VS.85%29.aspx */
47 FPO MACRO cdwLocals, cdwParams, cbProlog, cbRegs, fUseBP, cbFrame
48 .FPO (cdwLocals, cdwParams, cbProlog, cbRegs, fUseBP, cbFrame)
49 ENDM
50
51 /* MASM doesn't have an ASCII macro */
52 .ASCII MACRO text:VARARG
53 DB text
54 ENDM
55 .ascii MACRO text:VARARG
56 DB text
57 ENDM
58
59 /* MASM doesn't have an ASCIZ macro */
60 .ASCIZ MACRO text:VARARG
61 DB text
62 DB 0
63 ENDM
64 .asciz MACRO text:VARARG
65 DB text
66 DB 0
67 ENDM
68
69 .code64 MACRO
70 .code
71 ENDM
72
73 .code32 MACRO
74 .code
75 .586P
76 ENDM
77
78 .code16 MACRO
79 ASSUME nothing
80 .text SEGMENT use16
81 .586P
82 ENDM
83
84 .endcode16 MACRO
85 .text ENDS
86 ENDM
87
88 .bss MACRO
89 .DATA?
90 ASSUME nothing
91 ENDM
92
93 //.text MACRO
94 //ENDM
95
96 .align MACRO alignment
97 ALIGN alignment
98 ENDM
99
100 .byte MACRO args:VARARG
101 db args
102 ENDM
103
104 .short MACRO args:VARARG
105 dw args
106 ENDM
107
108 .word MACRO args:VARARG
109 dw args
110 ENDM
111
112 .long MACRO args:VARARG
113 dd args
114 ENDM
115
116 .double MACRO args:VARARG
117 dq args
118 ENDM
119
120 .org MACRO value
121 ORG value
122 ENDM
123
124 .fill MACRO count, size, value
125 REPEAT count
126 if (size EQ 1)
127 DB value
128 elseif (size EQ 2)
129 DW value
130 elseif (size EQ 4)
131 DD value
132 endif
133 ENDM
134 ENDM
135
136 .space MACRO count
137 DB 0 DUP (count)
138 ENDM
139
140 ljmp MACRO segment, offset
141 DB 0EAh
142 DD offset
143 DW segment
144 ENDM
145
146 ljmp16 MACRO segment, offset
147 DB 0EAh
148 DW offset
149 DW segment
150 ENDM
151
152 UNIMPLEMENTED MACRO name
153 ENDM
154
155 absolute MACRO address
156 __absolute__address__ = address
157 ENDM
158
159 resb MACRO name, size
160 name = __absolute__address__
161 __absolute__address__ = __absolute__address__ + size
162 ENDM
163
164
165 /* We need this to distinguish repeat from macros */
166 #define ENDR ENDM
167
168 #define CR 13
169 #define LF 10
170 #define NUL 0
171
172 #else /***********************************************************************/
173
174 /* Force intel syntax */
175 .intel_syntax noprefix
176
177 .altmacro
178
179 /* Hex numbers need to be in 0x1AB format */
180 #define HEX(y) 0x##y
181
182 /* Macro values need to be marked */
183 #define VAL(x) \x
184
185 /* Due to MASM's reverse syntax, we are forced to use a precompiler macro */
186 #define MACRO(...) .macro __VA_ARGS__
187 #define ENDM .endm
188
189 /* To avoid reverse syntax we provide a new macro .PROC, replacing PROC... */
190 .macro .PROC name
191 .func \name
192 _\name:
193 .cfi_startproc
194 .equ cfa_current_offset, -8
195 .endm
196
197 /* ... and .ENDP, replacing ENDP */
198 .macro .ENDP name
199 .cfi_endproc
200 .endfunc
201 .endm
202
203 /* MASM compatible PUBLIC */
204 .macro PUBLIC symbol
205 .global \symbol
206 .endm
207
208 /* Dummy ASSUME */
209 .macro ASSUME p1 p2 p3 p4 p5 p6 p7 p8
210 .endm
211
212 /* MASM needs an end tag for segments */
213 .macro .endcode16
214 .endm
215
216 /* MASM compatible ALIGN */
217 #define ALIGN .align
218
219 /* MASM compatible REPEAT, additional ENDR */
220 #define REPEAT .rept
221 #define ENDR .endr
222
223 .macro ljmp segment, offset
224 jmp far ptr \segment:\offset
225 .endm
226
227 .macro ljmp16 segment, offset
228 jmp far ptr \segment:\offset
229 .endm
230
231 /* MASM compatible EXTERN */
232 .macro EXTERN name
233 .endm
234
235 /* MASM needs an END tag */
236 #define END
237
238 .macro .MODEL model
239 .endm
240
241 .macro .code
242 .text
243 .endm
244
245 /* check http://msdn.microsoft.com/en-us/library/9c9k076y%28VS.80%29.aspx
246 and http://msdn.microsoft.com/en-us/library/ms679352%28VS.85%29.aspx */
247 .macro FPO cdwLocals, cdwParams, cbProlog, cbRegs, fUseBP, cbFrame
248 /* dummy */
249 .endm
250
251 /* Macros for x64 stack unwind OPs */
252
253 .macro .allocstack size
254 .cfi_adjust_cfa_offset \size
255 .set cfa_current_offset, cfa_current_offset - \size
256 .endm
257
258 code = 1
259 .macro .pushframe param=0
260 .if (\param)
261 .cfi_adjust_cfa_offset 0x30
262 .set cfa_current_offset, cfa_current_offset - 0x30
263 .else
264 .cfi_adjust_cfa_offset 0x28
265 .set cfa_current_offset, cfa_current_offset - 0x28
266 .endif
267 .endm
268
269 .macro .pushreg reg
270 .cfi_adjust_cfa_offset 8
271 .equ cfa_current_offset, cfa_current_offset - 8
272 .cfi_offset \reg, cfa_current_offset
273 .endm
274
275 .macro .savereg reg, offset
276 // checkme!!!
277 .cfi_offset \reg, \offset
278 .endm
279
280 .macro .savexmm128 reg, offset
281 // checkme!!!
282 .cfi_offset \reg, \offset
283 .endm
284
285 .macro .setframe reg, offset
286 .cfi_def_cfa reg, \offset
287 .equ cfa_current_offset, \offset
288 .endm
289
290 .macro .endprolog
291 .endm
292
293 .macro absolute address
294 __absolute__address__ = \address
295 ENDM
296
297 .macro resb name, size
298 \name = __absolute__address__
299 __absolute__address__ = __absolute__address__ + \size
300 ENDM
301
302 .macro UNIMPLEMENTED2 file, line, func
303
304 jmp 3f
305 1: .asciz "\func"
306 2: .asciz \file
307 3:
308 sub rsp, 0x20
309 lea rcx, MsgUnimplemented[rip]
310 lea rdx, 1b[rip]
311 lea r8, 2b[rip]
312 mov r9, \line
313 call DbgPrint
314 add rsp, 0x20
315 .endm
316 #define UNIMPLEMENTED UNIMPLEMENTED2 __FILE__, __LINE__,
317
318 /* MASM/ML uses ".if" for runtime conditionals, and "if" for compile time
319 conditionals. We therefore use "if", too. .if shouldn't be used at all */
320 #define if .if
321 #define endif .endif
322 #define else .else
323 #define elseif .elseif
324
325 #define CR "\r"
326 #define LF "\n"
327 #define NUL "\0"
328
329 #endif