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