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