[SDK] Provide .const macro for gas
[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 .quad MACRO args:VARARG
140 dq args
141 ENDM
142
143 .double MACRO args:VARARG
144 real8 args
145 ENDM
146
147 .org MACRO value
148 ORG value
149 ENDM
150
151 .fill MACRO count, size, value
152 REPEAT count
153 if (size EQ 1)
154 DB value
155 elseif (size EQ 2)
156 DW value
157 elseif (size EQ 4)
158 DD value
159 endif
160 ENDM
161 ENDM
162
163 .skip MACRO size, fill:=<0>
164 DB size DUP (fill)
165 ENDM
166
167 .space MACRO size, fill:=<0>
168 .skip size, fill
169 ENDM
170
171 ljmp MACRO segment, offset
172 DB 0EAh
173 DD offset
174 DW segment
175 ENDM
176
177 ljmp16 MACRO segment, offset
178 DB 0EAh
179 DW offset
180 DW segment
181 ENDM
182
183 data32 MACRO opcode:VARARG
184 DB 66h
185 opcode
186 ENDM
187
188 UNIMPLEMENTED MACRO name
189 ENDM
190
191 absolute MACRO address
192 __absolute__address__ = address
193 ENDM
194
195 resb MACRO name, size
196 name = __absolute__address__
197 __absolute__address__ = __absolute__address__ + size
198 ENDM
199
200 /* We need this to distinguish repeat from macros */
201 #define ENDR ENDM
202
203 #define CR 13
204 #define LF 10
205 #define NUL 0
206
207 /* For compatibility with GAS */
208 CFI_STARTPROC MACRO start
209 ENDM
210 CFI_ENDPROC MACRO
211 ENDM
212 CFI_DEF_CFA MACRO reg:REQ, offset:REQ
213 ENDM
214 CFI_DEF_CFA_OFFSET MACRO offset:REQ
215 ENDM
216 CFI_DEF_CFA_REGISTER MACRO reg:REQ
217 ENDM
218 CFI_ADJUST_CFA_OFFSET MACRO offset:REQ
219 ENDM
220 CFI_OFFSET MACRO reg:REQ, offset:REQ
221 ENDM
222 CFI_REGISTER MACRO reg1:REQ, reg2:REQ
223 ENDM
224 CFI_REL_OFFSET MACRO reg:REQ, offset:REQ
225 ENDM
226 CFI_SAME_VALUE MACRO reg:REQ
227 ENDM
228
229 #else /***********************************************************************/
230
231 /* Force intel syntax */
232 .intel_syntax noprefix
233
234 .altmacro
235
236 /* Explicit radix in GAS syntax */
237 #define BIN(x) 0b##x
238 #define OCT(x) 0##x
239 #define DEC(x) x
240 #define HEX(x) 0x##x
241
242 /* Macro values need to be marked */
243 #define VAL(x) \x
244
245 /* Due to MASM's reverse syntax, we are forced to use a precompiler macro */
246 #define MACRO(...) .macro __VA_ARGS__
247 #define ENDM .endm
248
249 /* To avoid reverse syntax we provide a new macro .PROC, replacing PROC... */
250 .macro .PROC name
251 .func \name
252 #ifdef _X86_
253 /* x86 GAS expects a label with _ prefix */
254 _\name:
255 #endif
256 \name:
257 .cfi_startproc
258 .equ cfa_current_offset, -8
259 .endm
260 #define FUNC .PROC
261
262 /* ... and .ENDP, replacing ENDP */
263 .macro .ENDP
264 .cfi_endproc
265 .endfunc
266 .endm
267 #define ENDFUNC .ENDP
268
269 /* MASM compatible PUBLIC */
270 .macro PUBLIC symbol
271 .global \symbol
272 .endm
273
274 /* No special marking of global labels */
275 .macro GLOBAL_LABEL label
276 \label:
277 .endm
278
279 /* Dummy ASSUME */
280 .macro ASSUME p1 p2 p3 p4 p5 p6 p7 p8
281 .endm
282
283 /* MASM needs an end tag for segments */
284 .macro .endcode16
285 .endm
286
287 /* MASM compatible ALIGN */
288 #define ALIGN .align
289
290 /* MASM compatible REPEAT, additional ENDR */
291 #define REPEAT .rept
292 #define ENDR .endr
293
294 .macro ljmp segment, offset
295 jmp far ptr \segment:\offset
296 .endm
297
298 .macro ljmp16 segment, offset
299 jmp far ptr \segment:\offset
300 .endm
301
302 /* MASM compatible EXTERN */
303 .macro EXTERN name
304 .endm
305
306 /* MASM needs an END tag */
307 #define END
308
309 .macro .MODEL model
310 .endm
311
312 .macro .code
313 .text
314 .endm
315
316 .macro .const
317 .section .rdata
318 .endm
319
320 /* check http://msdn.microsoft.com/en-us/library/9c9k076y%28VS.80%29.aspx
321 and http://msdn.microsoft.com/en-us/library/ms679352%28VS.85%29.aspx */
322 .macro FPO cdwLocals, cdwParams, cbProlog, cbRegs, fUseBP, cbFrame
323 .if (cbFrame == FRAME_TRAP)
324 .cfi_signal_frame
325 .endif
326 .endm
327
328 /* Macros for x64 stack unwind OPs */
329
330 .macro .allocstack size
331 .cfi_adjust_cfa_offset \size
332 .set cfa_current_offset, cfa_current_offset - \size
333 .endm
334
335 code = 1
336 .macro .pushframe param=0
337 .if (\param)
338 .cfi_adjust_cfa_offset 0x30
339 .set cfa_current_offset, cfa_current_offset - 0x30
340 .else
341 .cfi_adjust_cfa_offset 0x28
342 .set cfa_current_offset, cfa_current_offset - 0x28
343 .endif
344 .endm
345
346 .macro .pushreg reg
347 .cfi_adjust_cfa_offset 8
348 .equ cfa_current_offset, cfa_current_offset - 8
349 .cfi_offset \reg, cfa_current_offset
350 .endm
351
352 .macro .savereg reg, offset
353 // checkme!!!
354 .cfi_offset \reg, \offset
355 .endm
356
357 .macro .savexmm128 reg, offset
358 // checkme!!!
359 .cfi_offset \reg, \offset
360 .endm
361
362 .macro .setframe reg, offset
363 .cfi_def_cfa reg, \offset
364 .equ cfa_current_offset, \offset
365 .endm
366
367 .macro .endprolog
368 .endm
369
370 .macro absolute address
371 __absolute__address__ = \address
372 .endm
373
374 .macro resb name, size
375 \name = __absolute__address__
376 __absolute__address__ = __absolute__address__ + \size
377 .endm
378
379 .macro UNIMPLEMENTED2 file, line, func
380 jmp 3f
381 1: .asciz "\func"
382 2: .asciz \file
383 3:
384 sub rsp, 0x20
385 lea rcx, MsgUnimplemented[rip]
386 lea rdx, 1b[rip]
387 lea r8, 2b[rip]
388 mov r9, \line
389 call DbgPrint
390 add rsp, 0x20
391 .endm
392 #define UNIMPLEMENTED UNIMPLEMENTED2 __FILE__, __LINE__,
393
394 /* MASM/ML uses ".if" for runtime conditionals, and "if" for compile time
395 conditionals. We therefore use "if", too. .if shouldn't be used at all */
396 #define if .if
397 #define endif .endif
398 #define else .else
399 #define elseif .elseif
400
401 #define CR "\r"
402 #define LF "\n"
403 #define NUL "\0"
404
405 /* CFI annotations */
406 #define CFI_STARTPROC .cfi_startproc
407 #define CFI_ENDPROC .cfi_endproc
408 #define CFI_DEF_CFA .cfi_def_cfa
409 #define CFI_DEF_CFA_OFFSET .cfi_def_cfa_offset
410 #define CFI_DEF_CFA_REGISTER .cfi_def_cfa_register
411 #define CFI_ADJUST_CFA_OFFSET .cfi_adjust_cfa_offset
412 #define CFI_OFFSET .cfi_offset
413 #define CFI_REGISTER .cfi_register
414 #define CFI_REL_OFFSET .cfi_rel_offset
415 #define CFI_SAME_VALUE .cfi_same_value
416
417 #endif
418
419 #endif /* __ASM_INC__ */