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