GuestHost/installation/VBoxWinDrvInst.cpp: Try harder if DiInstallDriverW() returns...
[vbox.git] / include / iprt / asmdefs.mac
blob014e193e086dc32fafb360b4ac3f6e97c9901ea0
1 ;; @file
2 ; IPRT - Global YASM/NASM macros
6 ; Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 ; This file is part of VirtualBox base platform packages, as
9 ; available from https://www.virtualbox.org.
11 ; This program is free software; you can redistribute it and/or
12 ; modify it under the terms of the GNU General Public License
13 ; as published by the Free Software Foundation, in version 3 of the
14 ; License.
16 ; This program is distributed in the hope that it will be useful, but
17 ; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ; General Public License for more details.
21 ; You should have received a copy of the GNU General Public License
22 ; along with this program; if not, see <https://www.gnu.org/licenses>.
24 ; The contents of this file may alternatively be used under the terms
25 ; of the Common Development and Distribution License Version 1.0
26 ; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 ; in the VirtualBox distribution, in which case the provisions of the
28 ; CDDL are applicable instead of those of the GPL.
30 ; You may elect to license modified versions of this file under the
31 ; terms and conditions of either the GPL or the CDDL or both.
33 ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
36 ; Special hack for bs3kit.
37 %ifdef RT_ASMDEFS_INC_FIRST_FILE
38  %include "asmdefs-first.mac" ; edk2-ignore-hack
39 %endif
41 %ifndef ___iprt_asmdefs_mac
42 %define ___iprt_asmdefs_mac
45 ;; @defgroup grp_rt_cdefs_size     Size Constants
46 ; (Of course, these are binary computer terms, not SI.)
47 ; @{
48 ;; 1 K (Kilo)                     (1 024).
49 %define _1K                     000000400h
50 ;; 4 K (Kilo)                     (4 096).
51 %define _4K                     000001000h
52 ;; 8 K (Kilo)                     (8 192).
53 %define _8K                     000002000h
54 ;; 16 K (Kilo)                   (16 384).
55 %define _16K                    000004000h
56 ;; 32 K (Kilo)                   (32 768).
57 %define _32K                    000008000h
58 ;; 64 K (Kilo)                   (65 536).
59 %define _64K                    000010000h
60 ;; 128 K (Kilo)                 (131 072).
61 %define _128K                   000020000h
62 ;; 256 K (Kilo)                 (262 144).
63 %define _256K                   000040000h
64 ;; 512 K (Kilo)                 (524 288).
65 %define _512K                   000080000h
66 ;; 1 M (Mega)                 (1 048 576).
67 %define _1M                     000100000h
68 ;; 2 M (Mega)                 (2 097 152).
69 %define _2M                     000200000h
70 ;; 4 M (Mega)                 (4 194 304).
71 %define _4M                     000400000h
72 ;; 1 G (Giga)             (1 073 741 824).
73 %define _1G                     040000000h
74 ;; 2 G (Giga)             (2 147 483 648).
75 %define _2G             00000000080000000h
76 ;; 4 G (Giga)             (4 294 967 296).
77 %define _4G             00000000100000000h
78 ;; 1 T (Tera)         (1 099 511 627 776).
79 %define _1T             00000010000000000h
80 ;; 1 P (Peta)     (1 125 899 906 842 624).
81 %define _1P             00004000000000000h
82 ;; 1 E (Exa)  (1 152 921 504 606 846 976).
83 %define _1E             01000000000000000h
84 ;; 2 E (Exa)  (2 305 843 009 213 693 952).
85 %define _2E             02000000000000000h
86 ;; @}
90 ; Define RT_STRICT for debug builds like iprt/cdefs.h does.
91 %ifndef RT_STRICT
92  %ifdef DEBUG
93   %define RT_STRICT
94  %endif
95 %endif
96 %ifdef RT_NO_STRICT
97  %undef RT_STRICT
98 %endif
101 ; Make the mask for the given bit.
102 %define RT_BIT(bit)        (1 << bit)
105 ; Make the mask for the given bit.
106 %define RT_BIT_32(bit)     (1 << bit)
108 ; Make the mask for the given bit.
109 %define RT_BIT_64(bit)     (1 << bit)
112 ; Makes a 32-bit unsigned (not type safe, but whatever) out of four byte values.
113 %define RT_MAKE_U32_FROM_U8(b0, b1, b2, b3) ( (b3 << 24) | (b2 << 16) | (b1 << 8) | b0 )
115 ;; Preprocessor concatenation macro.
116 %define RT_CONCAT(a_1,a_2)              a_1 %+ a_2
118 ;; Preprocessor concatenation macro, three arguments.
119 %define RT_CONCAT3(a_1,a_2,a_3)         a_1 %+ a_2 %+ a_3
121 ;; Preprocessor concatenation macro, four arguments.
122 %define RT_CONCAT4(a_1,a_2,a_3,a_4)     a_1 %+ a_2 %+ a_3 %+ a_4
125 ; Trick for using RT_CONCAT and the like on %define names.
126 ; @param 1  The name (expression.
127 ; @param 2  The value.
128 %macro RT_DEFINE_EX 2
129  %error 1=%1 2=%2
130  %define %1 %2
131 %endmacro
134 ; Trick for using RT_CONCAT and the like on %xdefine names.
135 ; @param 1  The name (expression.
136 ; @param 2  The value.
137 %macro RT_XDEFINE_EX 2
138  %xdefine %1 %2
139 %endmacro
142 ; Trick for using RT_CONCAT and the like on %undef names.
143 ; @param 1  The name (expression.
144 %macro RT_UNDEF_EX 1
145  %error 1=%1
146  %undef %1
147 %endmacro
150 ; Empty define
151 %define RT_NOTHING
153 ;; Define ASM_FORMAT_PE64 if applicable.
154 %ifdef ASM_FORMAT_PE
155  %ifdef RT_ARCH_AMD64
156   %define ASM_FORMAT_PE64       1
157  %endif
158 %endif
161 ; SEH64 macros.
162 %ifdef RT_ASM_WITH_SEH64
163  %ifndef ASM_FORMAT_PE64
164   %undef RT_ASM_WITH_SEH64
165  %endif
166 %endif
168 %ifdef RT_ASM_WITH_SEH64_ALT
169  %ifdef ASM_FORMAT_PE64
170   ;; @name Register numbers. Used with RT_CONCAT to convert macro inputs to numbers.
171   ;; @{
172   %define SEH64_PE_GREG_rax     0
173   %define SEH64_PE_GREG_xAX     0
174   %define SEH64_PE_GREG_rcx     1
175   %define SEH64_PE_GREG_xCX     1
176   %define SEH64_PE_GREG_rdx     2
177   %define SEH64_PE_GREG_xDX     2
178   %define SEH64_PE_GREG_rbx     3
179   %define SEH64_PE_GREG_xBX     3
180   %define SEH64_PE_GREG_rsp     4
181   %define SEH64_PE_GREG_xSP     4
182   %define SEH64_PE_GREG_rbp     5
183   %define SEH64_PE_GREG_xBP     5
184   %define SEH64_PE_GREG_rsi     6
185   %define SEH64_PE_GREG_xSI     6
186   %define SEH64_PE_GREG_rdi     7
187   %define SEH64_PE_GREG_xDI     7
188   %define SEH64_PE_GREG_r8      8
189   %define SEH64_PE_GREG_r9      9
190   %define SEH64_PE_GREG_r10     10
191   %define SEH64_PE_GREG_r11     11
192   %define SEH64_PE_GREG_r12     12
193   %define SEH64_PE_GREG_r13     13
194   %define SEH64_PE_GREG_r14     14
195   %define SEH64_PE_GREG_r15     15
196   ;; @}
198   ;; @name PE unwind operations.
199   ;; @{
200   %define SEH64_PE_PUSH_NONVOL      0
201   %define SEH64_PE_ALLOC_LARGE      1
202   %define SEH64_PE_ALLOC_SMALL      2
203   %define SEH64_PE_SET_FPREG        3
204   %define SEH64_PE_SAVE_NONVOL      4
205   %define SEH64_PE_SAVE_NONVOL_FAR  5
206   %define SEH64_PE_SAVE_XMM128      8
207   %define SEH64_PE_SAVE_XMM128_FAR  9
208   ;; @}
210   ;;
211   ; Starts the unwind info for the manual SEH64 info generation.
212   ; @param 1 Function name.
213   %macro SEH64_ALT_START_UNWIND_INFO 1
214    %assign seh64_idxOps   0
215    %assign seh64_FrameReg SEH64_PE_GREG_rsp
216    %assign seh64_offFrame 0
217    %define asm_seh64_proc %1
218    %undef  seh64_slot_bytes
219   %endmacro
221   ;; We keep the unwind bytes in the seh64_slot_bytes (x)define, in reverse order as per spec.
222   %macro SEH64_APPEND_SLOT_PAIR 2
223    %ifdef seh64_slot_bytes
224     %xdefine seh64_slot_bytes %1, %2, seh64_slot_bytes
225    %else
226     %xdefine seh64_slot_bytes %1, %2
227    %endif
228   %endmacro
230   ;; For multi-slot unwind info.
231   %macro SEH64_APPEND_SLOT_BYTES 2+
232    %rep %0
233     %rotate -1
234     %ifdef seh64_slot_bytes
235      %xdefine seh64_slot_bytes %1, seh64_slot_bytes
236     %else
237      %xdefine seh64_slot_bytes %1
238     %endif
239    %endrep
240   %endmacro
242  %else
243   %undef RT_ASM_WITH_SEH64_ALT
244  %endif
245 %endif
248 ; Records a xBP push.
249 %macro SEH64_PUSH_xBP 0
250  %ifdef RT_ASM_WITH_SEH64
251         [pushreg rbp]
253  %elifdef RT_ASM_WITH_SEH64_ALT
254 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
255   %ifdef ASM_FORMAT_PE64
256    SEH64_APPEND_SLOT_PAIR   RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
257                             SEH64_PE_PUSH_NONVOL | (SEH64_PE_GREG_rbp << 4)
258   %endif
259   %assign seh64_idxOps      seh64_idxOps + 1
260  %endif
261 %endmacro
264 ; Records a general register push.
265 ; @param 1 Register name.
266 %macro SEH64_PUSH_GREG 1
267  %ifdef RT_ASM_WITH_SEH64
268         [pushreg %1]
270  %elifdef RT_ASM_WITH_SEH64_ALT
271 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
272   %ifdef ASM_FORMAT_PE64
273    SEH64_APPEND_SLOT_PAIR   RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
274                             SEH64_PE_PUSH_NONVOL | (RT_CONCAT(SEH64_PE_GREG_,%1) << 4)
275   %endif
276   %assign seh64_idxOps      seh64_idxOps + 1
277  %endif
278 %endmacro
281 ; Sets xBP as frame pointer that's pointing to a stack position %1 relative to xBP.
282 %macro SEH64_SET_FRAME_xBP 1
283  %ifdef RT_ASM_WITH_SEH64
284   [setframe rbp, %1]
286  %elifdef RT_ASM_WITH_SEH64_ALT
287 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
288   %ifdef ASM_FORMAT_PE64
289    SEH64_APPEND_SLOT_PAIR   RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
290                             SEH64_PE_SET_FPREG | 0 ; vs2019 seems to put the offset in the info field
291    %assign seh64_FrameReg   SEH64_PE_GREG_rbp
292    %assign seh64_offFrame   %1
293   %endif
294   %assign seh64_idxOps      seh64_idxOps + 1
295  %endif
296 %endmacro
299 ; Records an ADD xSP, %1.
300 %macro SEH64_ALLOCATE_STACK 1
301  %ifdef RT_ASM_WITH_SEH64
302         [allocstack %1]
304  %elifdef RT_ASM_WITH_SEH64_ALT
305 RT_CONCAT(.seh64_op_label_,seh64_idxOps):
306   %ifdef ASM_FORMAT_PE64
307    %if (%1) & 7
308     %error "SEH64_ALLOCATE_STACK must be a multiple of 8"
309    %endif
310    %if (%1) < 8
311     %error "SEH64_ALLOCATE_STACK must have an argument that's 8 or higher."
312    %elif (%1) <= 128
313     SEH64_APPEND_SLOT_PAIR RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
314                            SEH64_PE_ALLOC_SMALL | ((((%1) / 8) - 1) << 4)
315    %elif (%1) < 512
316     SEH64_APPEND_SLOT_BYTES RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
317                             SEH64_PE_ALLOC_LARGE | 0, \
318                             ((%1) / 8) & 0xff, ((%1) / 8) >> 8
319    %else
320     SEH64_APPEND_SLOT_BYTES RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
321                             SEH64_PE_ALLOC_LARGE | 1, \
322                             (%1) & 0xff, ((%1) >> 8) & 0xff, ((%1) >> 16) & 0xff, ((%1) >> 24) & 0xff
323    %endif
324   %endif
325   %assign seh64_idxOps      seh64_idxOps + 1
326  %endif
327 %endmacro
329 %macro SEH64_INFO_HELPER 1
330 %if defined(%1)
331         dw      %1
332 %endif
333 %endmacro
336 ; Ends the prologue.
337 %macro SEH64_END_PROLOGUE 0
338 .end_of_prologue:
339  %ifdef RT_ASM_WITH_SEH64
340         [endprolog]
342  %elifdef RT_ASM_WITH_SEH64_ALT
343   %ifdef ASM_FORMAT_PE
344         ; Emit the unwind info now.
345    %ifndef ASM_DEFINED_XDATA_SECTION
346     %define ASM_DEFINED_XDATA_SECTION
347         section .xdata rdata align=4
348    %else
349         section .xdata
350         align   4, db 0
351    %endif
352 .unwind_info:
353         db      1                       ; version 1 (3 bit), no flags (5 bits)
354         db      .end_of_prologue - .start_of_prologue
356         db      (.unwind_info_array_end - .unwind_info_array) / 2
357         db      seh64_FrameReg | (seh64_offFrame & 0xf0) ; framereg and offset/16.
358 .unwind_info_array:
359    %ifdef seh64_slot_bytes
360         db      seh64_slot_bytes
361     %undef seh64_slot_bytes
362    %endif
363 .unwind_info_array_end:
365         ; Reset the segment
366         BEGINCODE
367   %endif
368  %endif
369 %endmacro
372 ; Macro for generating the endbr32/64 instruction when
373 ; RT_WITH_IBT_BRANCH_PROTECTION is defined.
374 %macro IBT_ENDBRxx 0
375  %ifdef RT_WITH_IBT_BRANCH_PROTECTION
376   %ifdef RT_ARCH_AMD64
377         db      0xf3, 0x0f, 0x1e, 0xfa ; yasm doesn't know about endbr64
378   %elifdef RT_ARCH_X86
379         db      0xf3, 0x0f, 0x1e, 0xfb ; yasm doesn't know about endbr32
380   %else
381    %error "Which arch?"
382   %endif
383  %endif
384 %endmacro
387 ; Macro for generating the endbr32/64 instruction when
388 ; RT_WITH_IBT_BRANCH_PROTECTION_WITHOUT_NOTRACK is defined.
389 %macro IBT_ENDBRxx_WITHOUT_NOTRACK 0
390  %ifdef RT_WITH_IBT_BRANCH_PROTECTION_WITHOUT_NOTRACK
391   %ifdef RT_ARCH_AMD64
392         db      0xf3, 0x0f, 0x1e, 0xfa ; yasm doesn't know about endbr64
393   %elifdef RT_ARCH_X86
394         db      0xf3, 0x0f, 0x1e, 0xfb ; yasm doesn't know about endbr32
395   %else
396    %error "Which arch?"
397   %endif
398  %endif
399 %endmacro
402 ; Macro for generating a NOTRACK prefix to an indirect jmp or call
403 ; instruction when RT_WITH_IBT_BRANCH_PROTECTION is defined.
405 ; @note Carful if mixing with other segment prefixes (should work, but needs
406 ;       testing).
407 %macro IBT_NOTRACK 0
408  %ifdef RT_WITH_IBT_BRANCH_PROTECTION
409   %ifndef RT_WITH_IBT_BRANCH_PROTECTION_WITHOUT_NOTRACK
410         db      0x3e                    ; DS prefix.
411   %endif
412  %endif
413 %endmacro
417 ; Align code, pad with INT3.
418 %define ALIGNCODE(alignment) align alignment, db 0cch
421 ; Align data, pad with ZEROs.
422 %define ALIGNDATA(alignment) align alignment, db 0
425 ; Align BSS, pad with ZEROs.
426 %define ALIGNBSS(alignment) align alignment, resb 1
429 ; NAME_OVERLOAD can be defined by a .asm module to modify all the
430 ; names created using the name macros in this files.
431 ; This is handy when you've got some kind of template code.
432 %ifndef NAME_OVERLOAD
433  %ifdef RT_MANGLER_PREFIX
434   %define NAME_OVERLOAD(name) RT_MANGLER_PREFIX %+ name
435  %else
436   %define NAME_OVERLOAD(name) name
437  %endif
438 %endif
441 ; Mangles the given name so it can be referenced using DECLASM() in the
442 ; C/C++ world.
443 %ifndef ASM_FORMAT_BIN
444  %ifdef RT_ARCH_X86
445   %ifdef RT_OS_OS2
446    %define NAME(name)   _ %+ NAME_OVERLOAD(name)
447   %endif
448   %ifdef RT_OS_WINDOWS
449    %define NAME(name)   _ %+ NAME_OVERLOAD(name)
450   %endif
451  %endif
452  %ifdef RT_OS_DARWIN
453   %define NAME(name)    _ %+ NAME_OVERLOAD(name)
454  %endif
455 %endif
456 %ifndef NAME
457  %define NAME(name)     NAME_OVERLOAD(name)
458 %endif
461 ; Mangles the given C name so it will _import_ the right symbol.
462 %ifdef ASM_FORMAT_PE
463  %define IMPNAME(name)  __imp_ %+ NAME(name)
464 %else
465  %define IMPNAME(name)  NAME(name)
466 %endif
469 ; Gets the pointer to an imported object.
470 %ifdef ASM_FORMAT_PE
471  %ifdef RT_ARCH_AMD64
472   %define IMP(name)     qword [IMPNAME(name) wrt rip]
473  %else
474   %define IMP(name)     dword [IMPNAME(name)]
475  %endif
476 %else
477  %define IMP(name)      IMPNAME(name)
478 %endif
481 ; Gets the pointer to an imported object.
482 %ifdef ASM_FORMAT_PE
483  %ifdef RT_ARCH_AMD64
484   %define IMP_SEG(SegOverride, name)  qword [SegOverride:IMPNAME(name) wrt rip]
485  %else
486   %define IMP_SEG(SegOverride, name)  dword [SegOverride:IMPNAME(name)]
487  %endif
488 %else
489  %define IMP_SEG(SegOverride, name)   IMPNAME(name)
490 %endif
493 ; Declares an imported object for use with IMP2.
494 ; @note May change the current section!
495 %macro EXTERN_IMP2 1
496     extern IMPNAME(%1)
497     BEGINDATA
498  %ifdef ASM_FORMAT_MACHO
499     g_Imp2_ %+ %1:  RTCCPTR_DEF IMPNAME(%1)
500  %endif
501 %endmacro
504 ; Gets the pointer to an imported object, version 2.
505 %ifdef ASM_FORMAT_PE
506  %ifdef RT_ARCH_AMD64
507   %define IMP2(name)    qword [IMPNAME(name) wrt rip]
508  %else
509   %define IMP2(name)    dword [IMPNAME(name)]
510  %endif
511 %elifdef ASM_FORMAT_ELF
512  %ifdef PIC
513   %ifdef RT_ARCH_AMD64
514    %define IMP2(name)   qword [rel IMPNAME(name) wrt ..got]
515   %else
516    %define IMP2(name)   IMPNAME(name) wrt ..plt
517   %endif
518  %endif
519 %elifdef ASM_FORMAT_MACHO
520  %define IMP2(name)     RTCCPTR_PRE [g_Imp2_ %+ name xWrtRIP]
521 %endif
522 %ifndef IMP2
523  %define IMP2(name)     IMPNAME(name)
524 %endif
528 ; Define a label as given, with a '$' prepended to permit using instruction
529 ; names like fdiv as labels.
530 %macro SAFE_LABEL 1
531 $%1:
532 %endmacro
535 ; Global marker which is DECLASM() compatible.
536 %macro GLOBALNAME 1
537 %ifndef ASM_FORMAT_BIN
538 global NAME(%1)
539 %endif
540 SAFE_LABEL NAME(%1)
541 %endmacro
544 ; Global exported marker which is DECLASM() compatible.
545 %macro EXPORTEDNAME 1
546  %ifdef ASM_FORMAT_PE
547   export %1=NAME(%1)
548  %endif
549  %ifdef __NASM__
550   %ifdef ASM_FORMAT_OMF
551    export NAME(%1) NAME(%1)
552   %endif
553 %endif
554 GLOBALNAME %1
555 %endmacro
558 ; Same as GLOBALNAME_EX, but without the name mangling.
560 ; @param %1     The symbol name - subjected to NAME().
561 ; @param %2     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
562 ;               PE ignores all but 'function' (yasm only).  Other formats ignores
563 ;               this completely.
564 ; @param %3     Symbol visibility: 'hidden', 'protected', 'internal', and
565 ;                                   RT_NOTHING (for 'default' visibility).
566 ;               These are ELF attributes, but 'hidden' is translated to
567 ;               'private_extern' for the Macho-O format.
568 ;               Ignored by other formats.
570 %macro GLOBALNAME_RAW 3
571 %ifdef ASM_FORMAT_ELF
572 global %1:%2 %3
574 %elifdef ASM_FORMAT_PE
575  %ifidn %2,function
576   %ifdef __YASM__      ; nasm does not support any attributes, it errors out. So, nasm is no good with control flow guard atm.
577 global %1:function
578   %else
579 global %1
580   %endif
581  %else
582 global %1
583  %endif
585 %elifdef ASM_FORMAT_MACHO
586  %ifidn %3,hidden
587 global %1:private_extern
588  %else
589 global %1
590  %endif
592 %elifndef ASM_FORMAT_BIN
593 global %1
595 %endif
598 %endmacro
601 ; Global marker which is DECLASM() compatible.
603 ; @param %1     The symbol name - subjected to NAME().
604 ; @param %2     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
605 ;               PE ignores all but 'function' (yasm only).  Other formats ignores
606 ;               this completely.
607 ; @param %3     Symbol visibility: 'hidden', 'protected', 'internal', and
608 ;                                   RT_NOTHING (for 'default' visibility).
609 ;               These are ELF attributes, but 'hidden' is translated to
610 ;               'private_extern' for the Macho-O format.
611 ;               Ignored by other formats.
613 %macro GLOBALNAME_EX 3
614 GLOBALNAME_RAW NAME(%1), %2, %3
615 %endmacro
619 ; Global exported marker, raw version w/o automatic name mangling.
621 ; @param %1     The internal symbol name.
622 ; @param %2     The external symbol name.
623 ; @param %3     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
624 ;               PE ignores all but 'function' (yasm only).  Other formats ignores
625 ;               this completely.
627 %macro EXPORTEDNAME_RAW 3
628  %ifdef ASM_FORMAT_PE
629   export %2=%1
630  %endif
631  %ifdef __NASM__
632   %ifdef ASM_FORMAT_OMF
633    export %1 %2
634   %endif
635 %endif
636 GLOBALNAME_RAW %1, %3, RT_NOTHING
637 %endmacro
640 ; Global exported marker which is DECLASM() compatible.
642 ; @param %1     The symbol name - subjected to NAME().
643 ; @param %2     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
644 ;               PE ignores all but 'function' (yasm only).  Other formats ignores
645 ;               this completely.
647 %macro EXPORTEDNAME_EX 2
648 EXPORTEDNAME_RAW NAME(%1), %1, %2
649 %endmacro
653 ; Begins a procedure, raw version w/o automatic name mangling.
654 ; @param 1  The (raw) name.
655 ; @param 2  Whether to manually apply IBT_ENDBRxx (1) or
656 ;           not (0, default). Optional
657 %macro BEGINPROC_RAW 1-2 0
658  %ifdef RT_ASM_WITH_SEH64_ALT
659   SEH64_ALT_START_UNWIND_INFO %1
660  %endif
661  %ifdef RT_ASM_WITH_SEH64
662 global     %1:function
663 proc_frame %1
664  %else
665 GLOBALNAME_RAW %1, function, hidden
666  %endif
667 .start_of_prologue:
668  %if %2 == 0
669         IBT_ENDBRxx
670  %endif
671 %endmacro
674 ; Begins a C callable (DECLASM) procedure.
675 %macro BEGINPROC 1-2 0
676 BEGINPROC_RAW NAME(%1), %2
677 %endmacro
681 ; Begins a exported procedure, raw version w/o automatic name mangling.
682 ; @param 1  Internal name.
683 ; @param 2  Exported name.
684 ; @param 3  Whether to manually apply IBT_ENDBRxx (1) or
685 ;           not (0, default). Optional
686 %macro BEGINPROC_EXPORTED_RAW 2-3 0
687  %ifdef RT_ASM_WITH_SEH64_ALT
688   SEH64_ALT_START_UNWIND_INFO %1
689  %endif
690  %ifdef RT_ASM_WITH_SEH64
691   %ifdef ASM_FORMAT_PE
692 export  %2=%1
693   %endif
694 global     %1:function
695 proc_frame %1
696  %else
697 EXPORTEDNAME_RAW %1, %2, function
698  %endif
699 .start_of_prologue:
700  %if %3 == 0
701         IBT_ENDBRxx
702  %endif
703 %endmacro
706 ; Begins a C callable (DECLASM) exported procedure.
707 %macro BEGINPROC_EXPORTED 1-2 0
708 BEGINPROC_EXPORTED_RAW NAME(%1), %1, %2
709 %endmacro
713 ; Ends a procedure, raw version w/o automatic name mangling.
714 %macro ENDPROC_RAW 1
715  %ifdef RT_ASM_WITH_SEH64
716 endproc_frame
717  %endif
718 GLOBALNAME_RAW %1 %+ _EndProc, , hidden ; no function here, this isn't a valid code flow target.
719 %ifdef ASM_FORMAT_ELF
720  %ifndef __NASM__ ; nasm does this in the global directive.
721 size %1               %1 %+ _EndProc - %1
722 size %1 %+ _EndProc   4 ; make it non-zero to shut up warnigns from Linux's objtool.
723  %endif
724 %endif
725     db  0xCC, 0xCC, 0xCC, 0xCC
727  %ifdef RT_ASM_WITH_SEH64_ALT
728   %ifdef ASM_FORMAT_PE
729         ; Emit the RUNTIME_FUNCTION entry.  The linker is picky here, no label.
730    %ifndef ASM_DEFINED_PDATA_SECTION
731     %define ASM_DEFINED_PDATA_SECTION
732         section .pdata rdata align=4
733    %else
734         section .pdata
735    %endif
736         dd      %1                    wrt ..imagebase
737         dd      %1 %+ _EndProc        wrt ..imagebase
738         dd      %1 %+ .unwind_info    wrt ..imagebase
740         ; Restore code section.
741         BEGINCODE
742   %endif
743  %endif
744 %endmacro
747 ; Ends a C callable procedure.
748 %macro ENDPROC 1
749 ENDPROC_RAW NAME(%1)
750 %endmacro
754 ; Do OMF and Mach-O/Yasm segment definitions
756 ; Both format requires this to get the segment order right, in the Mach-O/Yasm case
757 ; it's only to make sure the .bss section ends up last (it's not declared here).
759 %ifdef ASM_FORMAT_OMF
760  %ifndef RT_NOINC_SEGMENTS
762   ; 16-bit segments first (OMF / OS/2 specific).
763   %ifdef RT_INCL_16BIT_SEGMENTS
764    segment DATA16 public CLASS=FAR_DATA align=16 use16
765    segment DATA16_INIT public CLASS=FAR_DATA align=16 use16
766    group DGROUP16 DATA16 DATA16_INIT
768    ;;
769    ; Begins 16-bit data
770    %macro BEGINDATA16 0
771     segment DATA16
772    %endmacro
774    ;;
775    ; Begins 16-bit init data
776    %macro BEGINDATA16INIT 0
777     segment DATA16_INIT
778    %endmacro
780    segment CODE16 public CLASS=FAR_CODE align=16 use16
781    segment CODE16_INIT public CLASS=FAR_CODE align=16 use16
782    group CGROUP16 CODE16 CODE16_INIT
784    ;;
785    ; Begins 16-bit code
786    %macro BEGINCODE16 0
787     segment CODE16
788    %endmacro
790    ;;
791    ; Begins 16-bit init code
792    %macro BEGINCODE16INIT 0
793     segment CODE16_INIT
794    %endmacro
796   %endif
798   ; 32-bit segments.
799   segment TEXT32 public CLASS=CODE align=16 use32 flat
800   segment DATA32 public CLASS=DATA align=16 use32 flat
801   segment BSS32  public CLASS=BSS  align=16 use32 flat
803   ; Make the TEXT32 segment default.
804   segment TEXT32
805  %endif ; RT_NOINC_SEGMENTS
806 %endif
808 %ifdef ASM_FORMAT_MACHO
809  %ifdef __YASM__
810   section .text
811   section .data
812  %endif
813 %endif
817 ; Begins code
818 %ifdef ASM_FORMAT_OMF
819  %macro BEGINCODE 0
820   segment TEXT32
821  %endmacro
822 %else
823 %macro BEGINCODE 0
824  section .text
825 %endmacro
826 %endif
829 ; Begins constant (read-only) data
831 ; @remarks  This is mapped to the CODE section/segment when there isn't
832 ;           any dedicated const section/segment. (There is code that
833 ;           assumes this, so don't try change it.)
834 %ifdef ASM_FORMAT_OMF
835  %macro BEGINCONST 0
836   segment TEXT32
837  %endmacro
838 %else
839  %macro BEGINCONST 0
840   %ifdef ASM_FORMAT_MACHO ;; @todo check the other guys too.
841    section .rodata
842   %else
843    section .text
844   %endif
845  %endmacro
846 %endif
849 ; Begins initialized data
850 %ifdef ASM_FORMAT_OMF
851  %macro BEGINDATA 0
852   segment DATA32
853  %endmacro
854 %else
855 %macro BEGINDATA 0
856  section .data
857 %endmacro
858 %endif
861 ; Begins uninitialized data
862 %ifdef ASM_FORMAT_OMF
863  %macro BEGINBSS 0
864   segment BSS32
865  %endmacro
866 %else
867 %macro BEGINBSS 0
868  section .bss
869 %endmacro
870 %endif
874 ;; @def ARCH_BITS
875 ; Defines the bit count of the current context.
876 %ifndef ARCH_BITS
877  %ifdef RT_ARCH_AMD64
878   %define ARCH_BITS 64
879  %else
880   %define ARCH_BITS 32
881  %endif
882 %endif
884 ;; @def HC_ARCH_BITS
885 ; Defines the host architechture bit count.
886 %ifndef HC_ARCH_BITS
887  %ifndef IN_RC
888   %define HC_ARCH_BITS ARCH_BITS
889  %else
890   %define HC_ARCH_BITS 32
891  %endif
892 %endif
894 ;; @def R3_ARCH_BITS
895 ; Defines the host ring-3 architechture bit count.
896 %ifndef R3_ARCH_BITS
897  %ifdef IN_RING3
898   %define R3_ARCH_BITS ARCH_BITS
899  %else
900   %define R3_ARCH_BITS HC_ARCH_BITS
901  %endif
902 %endif
904 ;; @def R0_ARCH_BITS
905 ; Defines the host ring-0 architechture bit count.
906 %ifndef R0_ARCH_BITS
907  %ifdef IN_RING0
908   %define R0_ARCH_BITS ARCH_BITS
909  %else
910   %define R0_ARCH_BITS HC_ARCH_BITS
911  %endif
912 %endif
914 ;; @def GC_ARCH_BITS
915 ; Defines the guest architechture bit count.
916 %ifndef GC_ARCH_BITS
917  %ifdef IN_RC
918   %define GC_ARCH_BITS ARCH_BITS
919  %else
920   %define GC_ARCH_BITS 32
921  %endif
922 %endif
926 ;; @def RTHCPTR_DEF
927 ; The pesudo-instruction used to declare an initialized pointer variable in the host context.
928 %if HC_ARCH_BITS == 64
929  %define RTHCPTR_DEF    dq
930 %else
931  %define RTHCPTR_DEF    dd
932 %endif
934 ;; @def RTHCPTR_RES
935 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
936 ; variable of the host context.
937 %if HC_ARCH_BITS == 64
938  %define RTHCPTR_RES    resq
939 %else
940  %define RTHCPTR_RES    resd
941 %endif
943 ;; @def RTHCPTR_PRE
944 ; The memory operand prefix used for a pointer in the host context.
945 %if HC_ARCH_BITS == 64
946  %define RTHCPTR_PRE    qword
947 %else
948  %define RTHCPTR_PRE    dword
949 %endif
951 ;; @def RTHCPTR_CB
952 ; The size in bytes of a pointer in the host context.
953 %if HC_ARCH_BITS == 64
954  %define RTHCPTR_CB     8
955 %else
956  %define RTHCPTR_CB     4
957 %endif
961 ;; @def RTR0PTR_DEF
962 ; The pesudo-instruction used to declare an initialized pointer variable in the ring-0 host context.
963 %if R0_ARCH_BITS == 64
964  %define RTR0PTR_DEF    dq
965 %else
966  %define RTR0PTR_DEF    dd
967 %endif
969 ;; @def RTR0PTR_RES
970 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
971 ; variable of the ring-0 host context.
972 %if R0_ARCH_BITS == 64
973  %define RTR0PTR_RES    resq
974 %else
975  %define RTR0PTR_RES    resd
976 %endif
978 ;; @def RTR0PTR_PRE
979 ; The memory operand prefix used for a pointer in the ring-0 host context.
980 %if R0_ARCH_BITS == 64
981  %define RTR0PTR_PRE    qword
982 %else
983  %define RTR0PTR_PRE    dword
984 %endif
986 ;; @def RTR0PTR_CB
987 ; The size in bytes of a pointer in the ring-0 host context.
988 %if R0_ARCH_BITS == 64
989  %define RTR0PTR_CB     8
990 %else
991  %define RTR0PTR_CB     4
992 %endif
996 ;; @def RTR3PTR_DEF
997 ; The pesudo-instruction used to declare an initialized pointer variable in the ring-3 host context.
998 %if R3_ARCH_BITS == 64
999  %define RTR3PTR_DEF    dq
1000 %else
1001  %define RTR3PTR_DEF    dd
1002 %endif
1004 ;; @def RTR3PTR_RES
1005 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1006 ; variable of the ring-3 host context.
1007 %if R3_ARCH_BITS == 64
1008  %define RTR3PTR_RES    resq
1009 %else
1010  %define RTR3PTR_RES    resd
1011 %endif
1013 ;; @def RTR3PTR_PRE
1014 ; The memory operand prefix used for a pointer in the ring-3 host context.
1015 %if R3_ARCH_BITS == 64
1016  %define RTR3PTR_PRE    qword
1017 %else
1018  %define RTR3PTR_PRE    dword
1019 %endif
1021 ;; @def RTR3PTR_CB
1022 ; The size in bytes of a pointer in the ring-3 host context.
1023 %if R3_ARCH_BITS == 64
1024  %define RTR3PTR_CB     8
1025 %else
1026  %define RTR3PTR_CB     4
1027 %endif
1031 ;; @def RTGCPTR_DEF
1032 ; The pesudo-instruction used to declare an initialized pointer variable in the guest context.
1033 %if GC_ARCH_BITS == 64
1034  %define RTGCPTR_DEF    dq
1035 %else
1036  %define RTGCPTR_DEF    dd
1037 %endif
1039 ;; @def RTGCPTR_RES
1040 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1041 ; variable of the guest context.
1042 %if GC_ARCH_BITS == 64
1043  %define RTGCPTR_RES    resq
1044 %else
1045  %define RTGCPTR_RES    resd
1046 %endif
1048 %define RTGCPTR32_RES   resd
1049 %define RTGCPTR64_RES   resq
1051 ;; @def RTGCPTR_PRE
1052 ; The memory operand prefix used for a pointer in the guest context.
1053 %if GC_ARCH_BITS == 64
1054  %define RTGCPTR_PRE    qword
1055 %else
1056  %define RTGCPTR_PRE    dword
1057 %endif
1059 ;; @def RTGCPTR_CB
1060 ; The size in bytes of a pointer in the guest context.
1061 %if GC_ARCH_BITS == 64
1062  %define RTGCPTR_CB     8
1063 %else
1064  %define RTGCPTR_CB     4
1065 %endif
1068 ;; @def RTRCPTR_DEF
1069 ; The pesudo-instruction used to declare an initialized pointer variable in the raw mode context.
1070 %define RTRCPTR_DEF    dd
1072 ;; @def RTRCPTR_RES
1073 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1074 ; variable of the raw mode context.
1075 %define RTRCPTR_RES    resd
1077 ;; @def RTRCPTR_PRE
1078 ; The memory operand prefix used for a pointer in the raw mode context.
1079 %define RTRCPTR_PRE    dword
1081 ;; @def RTRCPTR_CB
1082 ; The size in bytes of a pointer in the raw mode context.
1083 %define RTRCPTR_CB     4
1086 ;; @def RT_CCPTR_DEF
1087 ; The pesudo-instruction used to declare an initialized pointer variable in the current context.
1089 ;; @def RT_CCPTR_RES
1090 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
1091 ; variable of the current context.
1093 ;; @def RT_CCPTR_PRE
1094 ; The memory operand prefix used for a pointer in the current context.
1096 ;; @def RT_CCPTR_CB
1097 ; The size in bytes of a pointer in the current context.
1099 %ifdef IN_RC
1100  %define RTCCPTR_DEF        RTRCPTR_DEF
1101  %define RTCCPTR_RES        RTRCPTR_RES
1102  %define RTCCPTR_PRE        RTRCPTR_PRE
1103  %define RTCCPTR_CB         RTRCPTR_CB
1104 %else
1105  %ifdef IN_RING0
1106   %define RTCCPTR_DEF       RTR0PTR_DEF
1107   %define RTCCPTR_RES       RTR0PTR_RES
1108   %define RTCCPTR_PRE       RTR0PTR_PRE
1109   %define RTCCPTR_CB        RTR0PTR_CB
1110  %else
1111   %define RTCCPTR_DEF       RTR3PTR_DEF
1112   %define RTCCPTR_RES       RTR3PTR_RES
1113   %define RTCCPTR_PRE       RTR3PTR_PRE
1114   %define RTCCPTR_CB        RTR3PTR_CB
1115  %endif
1116 %endif
1120 ;; @def RTHCPHYS_DEF
1121 ; The pesudo-instruction used to declare an initialized host physical address.
1122 %define RTHCPHYS_DEF    dq
1124 ;; @def RTHCPTR_RES
1125 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized
1126 ; host physical address variable
1127 %define RTHCPHYS_RES    resq
1129 ;; @def RTHCPTR_PRE
1130 ; The memory operand prefix used for a host physical address.
1131 %define RTHCPHYS_PRE    qword
1133 ;; @def RTHCPHYS_CB
1134 ; The size in bytes of a host physical address.
1135 %define RTHCPHYS_CB     8
1139 ;; @def RTGCPHYS_DEF
1140 ; The pesudo-instruction used to declare an initialized guest physical address.
1141 %define RTGCPHYS_DEF    dq
1143 ;; @def RTGCPHYS_RES
1144 ; The pesudo-instruction used to declare (=reserve space for) an uninitialized
1145 ; guest physical address variable
1146 %define RTGCPHYS_RES    resq
1148 ;; @def RTGCPTR_PRE
1149 ; The memory operand prefix used for a guest physical address.
1150 %define RTGCPHYS_PRE    qword
1152 ;; @def RTGCPHYS_CB
1153 ; The size in bytes of a guest physical address.
1154 %define RTGCPHYS_CB     8
1159 ; The size of the long double C/C++ type.
1160 ; On 32-bit Darwin this is 16 bytes, on L4, Linux, OS/2 and Windows
1161 ; it's 12 bytes.
1162 ; @todo figure out what 64-bit Windows does (I don't recall right now).
1163 %ifdef RT_ARCH_X86
1164  %ifdef RT_OS_DARWIN
1165   %define RTLRD_CB          16
1166  %else
1167   %define RTLRD_CB          12
1168  %endif
1169 %else
1170   %define RTLRD_CB          16
1171 %endif
1173 ;; @name Floating point constants along the lines of the iprt/types.h types.
1174 ; @note YASM does support special  the __Infinity__ and __NaN__ nasm tokens.
1175 ; @{
1176 %define RTFLOAT32U_QNAN_PLUS    0x7fc00000
1177 %define RTFLOAT32U_QNAN_MINUS   0xffc00000
1178 %define RTFLOAT32U_INF_PLUS     0x7f800000
1179 %define RTFLOAT32U_INF_MINUS    0xff800000
1181 %define RTFLOAT64U_QNAN_PLUS    0x7ff8000000000000
1182 %define RTFLOAT64U_QNAN_MINUS   0xfff8000000000000
1183 %define RTFLOAT64U_INF_PLUS     0x7ff0000000000000
1184 %define RTFLOAT64U_INF_MINUS    0xfff0000000000000
1185 ; @}
1189 ;; @def ASM_CALL64_GCC
1190 ; Indicates that we're using the GCC 64-bit calling convention.
1191 ; @see @ref sec_vboxrem_amd64_compare (in VBoxREMWrapper.cpp) for an ABI description.
1193 ;; @def ASM_CALL64_MSC
1194 ; Indicates that we're using the Microsoft 64-bit calling convention (fastcall on steroids).
1195 ; @see @ref sec_vboxrem_amd64_compare (in VBoxREMWrapper.cpp) for an ABI description.
1197 ; Note: On X86 we're using cdecl unconditionally. There is not yet any common
1198 ; calling convention on AMD64, that's why we need to support two different ones.)
1200 %ifdef RT_ARCH_AMD64
1201  %ifndef ASM_CALL64_GCC
1202   %ifndef ASM_CALL64_MSC
1203    ; define it based on the object format.
1204    %ifdef ASM_FORMAT_PE
1205     %define ASM_CALL64_MSC
1206    %else
1207     %define ASM_CALL64_GCC
1208    %endif
1209   %endif
1210  %else
1211   ; sanity check.
1212   %ifdef ASM_CALL64_MSC
1213    %error "Only one of the ASM_CALL64_* defines should be defined!"
1214   %endif
1215  %endif
1216 %else
1217  ;later; %ifdef ASM_CALL64_GCC
1218  ;later;  %error "ASM_CALL64_GCC is defined without RT_ARCH_AMD64!" ASM_CALL64_GCC
1219  ;later; %endif
1220  ;later; %ifdef ASM_CALL64_MSC
1221  ;later;  %error "ASM_CALL64_MSC is defined without RT_ARCH_AMD64!" ASM_CALL64_MSC
1222  ;later; %endif
1223 %endif
1226 ;; @def RT_BEGINPROC
1227 ; Starts an IPRT procedure that should be exported unless IN_RT_STATIC is defined.
1229 ; @param    1       The function name.  Will apply NAME macro to it.
1230 %macro RT_BEGINPROC 1
1231  %ifdef IN_RT_STATIC
1232 BEGINPROC          %1, 0
1233  %else
1234 BEGINPROC_EXPORTED %1, 0
1235  %endif
1236 %endmacro ; RT_BEGINPROC
1239 ;; @def RT_NOCRT
1240 ; Symbol name wrapper for the No-CRT bits.
1242 ; In order to coexist in the same process as other CRTs, we need to
1243 ; decorate the symbols such that they don't conflict the ones in the
1244 ; other CRTs. The result of such conflicts / duplicate symbols can
1245 ; confuse the dynamic loader on unix like systems.
1247 ; @remark Always feed the name to this macro first and then pass the result
1248 ;         on to the next *NAME* macro.
1250 %ifndef RT_WITHOUT_NOCRT_WRAPPERS
1251  %define RT_NOCRT(name) nocrt_ %+ name
1252 %else
1253  %define RT_NOCRT(name) name
1254 %endif
1256 ;; @def RT_NOCRT_BEGINPROC
1257 ; Starts a NOCRT procedure, taking care of name wrapping and aliasing.
1259 ; Weak aliases for regular crt (%1) names to the nocrt_ prefixed ones will be
1260 ; added when RT_WITH_NOCRT_ALIASES is defined and the output is ELF.  If
1261 ; RT_WITH_GENALIAS_NOCRT_ALIASES is undefined, strong aliases will be added for
1262 ; for non-ELF targets, otherwise it's assumed the genalias build tool will do
1263 ; the weak aliasing for us.
1265 %macro RT_NOCRT_BEGINPROC 1
1266  %ifdef RT_WITH_NOCRT_ALIASES
1267   %ifdef IN_RT_STATIC
1268 BEGINPROC          RT_NOCRT(%1), 1 ; Do our own IBT_ENDBRxx after aliasing/
1269   %else
1270 BEGINPROC_EXPORTED RT_NOCRT(%1), 1 ; Do our own IBT_ENDBRxx after aliasing/
1271   %endif
1272   %ifdef ASM_FORMAT_ELF
1273    ; ELF
1274    %ifdef RT_WITH_NOCRT_UNDERSCORE_ALIASES
1275 global NAME(_ %+ %1):function
1276 weak NAME(_ %+ %1)
1277 SAFE_LABEL NAME(_ %+ %1)
1278    %endif
1279 global NAME(%1):function
1280 weak NAME(%1)
1281 SAFE_LABEL NAME(%1)
1283   %elifndef RT_WITH_GENALIAS_NOCRT_ALIASES
1284    ; non-ELF when not using genalias.
1285    %ifdef RT_WITH_NOCRT_UNDERSCORE_ALIASES
1286 GLOBALNAME _%1
1287    %endif
1288 GLOBALNAME %1
1289   %endif
1290         IBT_ENDBRxx
1291  %else  ; !RT_WITH_NOCRT_ALIASES
1292   %ifdef IN_RT_STATIC
1293 BEGINPROC          RT_NOCRT(%1), 0
1294   %else
1295 BEGINPROC_EXPORTED RT_NOCRT(%1), 0
1296   %endif
1297  %endif ; !RT_WITH_NOCRT_ALIASES
1298 %endmacro ; RT_NOCRT_BEGINPROC
1302 ;; @def xCB
1303 ; The stack unit size  / The register unit size.
1305 ;; @def xSP
1306 ; The stack pointer register (RSP or ESP).
1308 ;; @def xBP
1309 ; The base pointer register (RBP or ESP).
1311 ;; @def xAX
1312 ; RAX or EAX depending on context.
1314 ;; @def xBX
1315 ; RBX or EBX depending on context.
1317 ;; @def xCX
1318 ; RCX or ECX depending on context.
1320 ;; @def xDX
1321 ; RDX or EDX depending on context.
1323 ;; @def xDI
1324 ; RDI or EDI depending on context.
1326 ;; @def xSI
1327 ; RSI or ESI depending on context.
1329 ;; @def xWrtRIP
1330 ; 'wrt rip' for AMD64 targets, nothing for x86 ones.
1332 %ifdef RT_ARCH_AMD64
1333  %define xCB 8
1334  %define xSP rsp
1335  %define xBP rbp
1336  %define xAX rax
1337  %define xBX rbx
1338  %define xCX rcx
1339  %define xDX rdx
1340  %define xDI rdi
1341  %define xSI rsi
1342  %define xWrtRIP wrt rip
1343 %else
1344  %define xCB 4
1345  %define xSP esp
1346  %define xBP ebp
1347  %define xAX eax
1348  %define xBX ebx
1349  %define xCX ecx
1350  %define xDX edx
1351  %define xDI edi
1352  %define xSI esi
1353  %define xWrtRIP
1354 %endif
1358 ; NASM sets __PASS__ to 0 in preprocess-only mode, and to 3 when only generating dependencies.
1359 ; YASM has no such setting which is why we must rely on kBuild to tell us what we're doing.
1360 ; For simplicity, we'll set the kBuild macro when using NASM.
1362 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1363  %ifdef __NASM__
1364   %if __PASS__ == 0 || __PASS__ == 3
1365    %define KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1366   %endif
1367  %endif
1368 %endif
1372 ; Some simple compile time assertions.
1374 ; Note! Requires new kBuild to work with YASM (see above).
1378 ; Structure size assertion macro.
1379 %define AssertCompileSize(a_Type, a_Size) AssertCompileSizeML a_Type, a_Size
1380 %macro AssertCompileSizeML 2
1381  %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1382   %assign AssertVar_cbActual   %1 %+ _size
1383   %assign AssertVar_cbExpected %2
1384   %if AssertVar_cbActual != AssertVar_cbExpected
1385    %error %1 is AssertVar_cbActual bytes instead of AssertVar_cbExpected
1386   %endif
1387  %endif
1388 %endmacro
1391 ; Structure size alignment assertion macro.
1393 %define AssertCompileSizeAlignment(a_Type, a_Align) AssertCompileSizeAlignmentML a_Type, a_Align
1394 %macro AssertCompileSizeAlignmentML 2
1395  %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1396   %assign AssertVar_cbActual    %1 %+ _size
1397   %assign AssertVar_cbAlignment %2
1398   %if (AssertVar_cbActual & (AssertVar_cbAlignment - 1)) != 0
1399    %error %1 is AssertVar_cbActual bytes, expected size with AssertVar_cbAlignment bytes alignment.
1400   %endif
1401  %endif
1402 %endmacro
1405 ; Structure member offset assertion macro.
1406 %define AssertCompileMemberOffset(a_Type, a_Member, a_off) AssertCompileMemberOffsetML a_Type, a_Member, a_off
1407 %macro AssertCompileMemberOffsetML 3
1408  %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1409   %assign AssertVar_offActual   %1 %+ . %+ %2
1410   %assign AssertVar_offExpected %3
1411   %if AssertVar_offActual != AssertVar_offExpected
1412    %error %1 %+ . %+ %2 is at AssertVar_offActual instead of AssertVar_offExpected
1413   %endif
1414  %endif
1415 %endmacro
1418 ; Structure member alignment assertion macro.
1419 %define AssertCompileMemberAlignment(a_Type, a_Member, a_cbAlign) AssertCompileMemberAlignmentML a_Type, a_Member, a_cbAlign
1420 %macro AssertCompileMemberAlignmentML 3
1421  %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1422   %assign AssertVar_offActual   %1 %+ . %+ %2
1423   %assign AssertVar_cbAlign     %3
1424   %if AssertVar_offActual & (AssertVar_cbAlign - 1)
1425    %error %1 %+ . %+ %2 is at AssertVar_offActual, expected AssertVar_cbAlign alignment
1426   %endif
1427  %endif
1428 %endmacro
1431 ; Generic compile time expression assertion.
1432 %define AssertCompile(a_Expr) AssertCompileML { a_Expr }
1433 %macro AssertCompileML 1
1434  %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
1435   %if (%1) != 1
1436    %assign AssertVar_uResult %1
1437    %error %1 => AssertVar_uResult
1438   %endif
1439  %endif
1440 %endmacro
1442 %endif