summaryrefslogtreecommitdiffstats
path: root/include/iprt/asmdefs.mac
blob: d798a0c046e63210063c26b490a27f5cd25e830d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
;; @file
; IPRT - Global YASM/NASM macros
;

;
; Copyright (C) 2006-2022 Oracle and/or its affiliates.
;
; This file is part of VirtualBox base platform packages, as
; available from https://www.virtualbox.org.
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation, in version 3 of the
; License.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, see <https://www.gnu.org/licenses>.
;
; The contents of this file may alternatively be used under the terms
; of the Common Development and Distribution License Version 1.0
; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
; in the VirtualBox distribution, in which case the provisions of the
; CDDL are applicable instead of those of the GPL.
;
; You may elect to license modified versions of this file under the
; terms and conditions of either the GPL or the CDDL or both.
;
; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
;

; Special hack for bs3kit.
%ifdef RT_ASMDEFS_INC_FIRST_FILE
 %include "asmdefs-first.mac"
%endif

%ifndef ___iprt_asmdefs_mac
%define ___iprt_asmdefs_mac


;; @defgroup grp_rt_cdefs_size     Size Constants
; (Of course, these are binary computer terms, not SI.)
; @{
;; 1 K (Kilo)                     (1 024).
%define _1K                     000000400h
;; 4 K (Kilo)                     (4 096).
%define _4K                     000001000h
;; 8 K (Kilo)                     (8 192).
%define _8K                     000002000h
;; 16 K (Kilo)                   (16 384).
%define _16K                    000004000h
;; 32 K (Kilo)                   (32 768).
%define _32K                    000008000h
;; 64 K (Kilo)                   (65 536).
%define _64K                    000010000h
;; 128 K (Kilo)                 (131 072).
%define _128K                   000020000h
;; 256 K (Kilo)                 (262 144).
%define _256K                   000040000h
;; 512 K (Kilo)                 (524 288).
%define _512K                   000080000h
;; 1 M (Mega)                 (1 048 576).
%define _1M                     000100000h
;; 2 M (Mega)                 (2 097 152).
%define _2M                     000200000h
;; 4 M (Mega)                 (4 194 304).
%define _4M                     000400000h
;; 1 G (Giga)             (1 073 741 824).
%define _1G                     040000000h
;; 2 G (Giga)             (2 147 483 648).
%define _2G             00000000080000000h
;; 4 G (Giga)             (4 294 967 296).
%define _4G             00000000100000000h
;; 1 T (Tera)         (1 099 511 627 776).
%define _1T             00000010000000000h
;; 1 P (Peta)     (1 125 899 906 842 624).
%define _1P             00004000000000000h
;; 1 E (Exa)  (1 152 921 504 606 846 976).
%define _1E             01000000000000000h
;; 2 E (Exa)  (2 305 843 009 213 693 952).
%define _2E             02000000000000000h
;; @}


;;
; Define RT_STRICT for debug builds like iprt/cdefs.h does.
%ifndef RT_STRICT
 %ifdef DEBUG
  %define RT_STRICT
 %endif
%endif
%ifdef RT_NO_STRICT
 %undef RT_STRICT
%endif

;;
; Make the mask for the given bit.
%define RT_BIT(bit)        (1 << bit)

;;
; Make the mask for the given bit.
%define RT_BIT_32(bit)     (1 << bit)
;;
; Make the mask for the given bit.
%define RT_BIT_64(bit)     (1 << bit)

;;
; Makes a 32-bit unsigned (not type safe, but whatever) out of four byte values.
%define RT_MAKE_U32_FROM_U8(b0, b1, b2, b3) ( (b3 << 24) | (b2 << 16) | (b1 << 8) | b0 )

;; Preprocessor concatenation macro.
%define RT_CONCAT(a_1,a_2)              a_1 %+ a_2

;; Preprocessor concatenation macro, three arguments.
%define RT_CONCAT3(a_1,a_2,a_3)         a_1 %+ a_2 %+ a_3

;; Preprocessor concatenation macro, four arguments.
%define RT_CONCAT4(a_1,a_2,a_3,a_4)     a_1 %+ a_2 %+ a_3 %+ a_4

;;
; Trick for using RT_CONCAT and the like on %define names.
; @param 1  The name (expression.
; @param 2  The value.
%macro RT_DEFINE_EX 2
 %error 1=%1 2=%2
 %define %1 %2
%endmacro

;;
; Trick for using RT_CONCAT and the like on %xdefine names.
; @param 1  The name (expression.
; @param 2  The value.
%macro RT_XDEFINE_EX 2
 %xdefine %1 %2
%endmacro

;;
; Trick for using RT_CONCAT and the like on %undef names.
; @param 1  The name (expression.
%macro RT_UNDEF_EX 1
 %error 1=%1
 %undef %1
%endmacro

;;
; Empty define
%define RT_NOTHING

;; Define ASM_FORMAT_PE64 if applicable.
%ifdef ASM_FORMAT_PE
 %ifdef RT_ARCH_AMD64
  %define ASM_FORMAT_PE64       1
 %endif
%endif

;;
; SEH64 macros.
%ifdef RT_ASM_WITH_SEH64
 %ifndef ASM_FORMAT_PE64
  %undef RT_ASM_WITH_SEH64
 %endif
%endif

%ifdef RT_ASM_WITH_SEH64_ALT
 %ifdef ASM_FORMAT_PE64
  ;; @name Register numbers. Used with RT_CONCAT to convert macro inputs to numbers.
  ;; @{
  %define SEH64_PE_GREG_rax     0
  %define SEH64_PE_GREG_xAX     0
  %define SEH64_PE_GREG_rcx     1
  %define SEH64_PE_GREG_xCX     1
  %define SEH64_PE_GREG_rdx     2
  %define SEH64_PE_GREG_xDX     2
  %define SEH64_PE_GREG_rbx     3
  %define SEH64_PE_GREG_xBX     3
  %define SEH64_PE_GREG_rsp     4
  %define SEH64_PE_GREG_xSP     4
  %define SEH64_PE_GREG_rbp     5
  %define SEH64_PE_GREG_xBP     5
  %define SEH64_PE_GREG_rsi     6
  %define SEH64_PE_GREG_xSI     6
  %define SEH64_PE_GREG_rdi     7
  %define SEH64_PE_GREG_xDI     7
  %define SEH64_PE_GREG_r8      8
  %define SEH64_PE_GREG_r9      9
  %define SEH64_PE_GREG_r10     10
  %define SEH64_PE_GREG_r11     11
  %define SEH64_PE_GREG_r12     12
  %define SEH64_PE_GREG_r13     13
  %define SEH64_PE_GREG_r14     14
  %define SEH64_PE_GREG_r15     15
  ;; @}

  ;; @name PE unwind operations.
  ;; @{
  %define SEH64_PE_PUSH_NONVOL      0
  %define SEH64_PE_ALLOC_LARGE      1
  %define SEH64_PE_ALLOC_SMALL      2
  %define SEH64_PE_SET_FPREG        3
  %define SEH64_PE_SAVE_NONVOL      4
  %define SEH64_PE_SAVE_NONVOL_FAR  5
  %define SEH64_PE_SAVE_XMM128      8
  %define SEH64_PE_SAVE_XMM128_FAR  9
  ;; @}

  ;;
  ; Starts the unwind info for the manual SEH64 info generation.
  ; @param 1 Function name.
  %macro SEH64_ALT_START_UNWIND_INFO 1
   %assign seh64_idxOps   0
   %assign seh64_FrameReg SEH64_PE_GREG_rsp
   %assign seh64_offFrame 0
   %define asm_seh64_proc %1
   %undef  seh64_slot_bytes
  %endmacro

  ;; We keep the unwind bytes in the seh64_slot_bytes (x)define, in reverse order as per spec.
  %macro SEH64_APPEND_SLOT_PAIR 2
   %ifdef seh64_slot_bytes
    %xdefine seh64_slot_bytes %1, %2, seh64_slot_bytes
   %else
    %xdefine seh64_slot_bytes %1, %2
   %endif
  %endmacro

  ;; For multi-slot unwind info.
  %macro SEH64_APPEND_SLOT_BYTES 2+
   %rep %0
    %rotate -1
    %ifdef seh64_slot_bytes
     %xdefine seh64_slot_bytes %1, seh64_slot_bytes
    %else
     %xdefine seh64_slot_bytes %1
    %endif
   %endrep
  %endmacro

 %else
  %undef RT_ASM_WITH_SEH64_ALT
 %endif
%endif

;;
; Records a xBP push.
%macro SEH64_PUSH_xBP 0
 %ifdef RT_ASM_WITH_SEH64
        [pushreg rbp]

 %elifdef RT_ASM_WITH_SEH64_ALT
RT_CONCAT(.seh64_op_label_,seh64_idxOps):
  %ifdef ASM_FORMAT_PE64
   SEH64_APPEND_SLOT_PAIR   RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
                            SEH64_PE_PUSH_NONVOL | (SEH64_PE_GREG_rbp << 4)
  %endif
  %assign seh64_idxOps      seh64_idxOps + 1
 %endif
%endmacro

;;
; Records a general register push.
; @param 1 Register name.
%macro SEH64_PUSH_GREG 1
 %ifdef RT_ASM_WITH_SEH64
        [pushreg %1]

 %elifdef RT_ASM_WITH_SEH64_ALT
RT_CONCAT(.seh64_op_label_,seh64_idxOps):
  %ifdef ASM_FORMAT_PE64
   SEH64_APPEND_SLOT_PAIR   RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
                            SEH64_PE_PUSH_NONVOL | (RT_CONCAT(SEH64_PE_GREG_,%1) << 4)
  %endif
  %assign seh64_idxOps      seh64_idxOps + 1
 %endif
%endmacro

;;
; Sets xBP as frame pointer that's pointing to a stack position %1 relative to xBP.
%macro SEH64_SET_FRAME_xBP 1
 %ifdef RT_ASM_WITH_SEH64
  [setframe rbp, %1]

 %elifdef RT_ASM_WITH_SEH64_ALT
RT_CONCAT(.seh64_op_label_,seh64_idxOps):
  %ifdef ASM_FORMAT_PE64
   SEH64_APPEND_SLOT_PAIR   RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
                            SEH64_PE_SET_FPREG | 0 ; vs2019 seems to put the offset in the info field
   %assign seh64_FrameReg   SEH64_PE_GREG_rbp
   %assign seh64_offFrame   %1
  %endif
  %assign seh64_idxOps      seh64_idxOps + 1
 %endif
%endmacro

;;
; Records an ADD xSP, %1.
%macro SEH64_ALLOCATE_STACK 1
 %ifdef RT_ASM_WITH_SEH64
        [allocstack %1]

 %elifdef RT_ASM_WITH_SEH64_ALT
RT_CONCAT(.seh64_op_label_,seh64_idxOps):
  %ifdef ASM_FORMAT_PE64
   %if (%1) & 7
    %error "SEH64_ALLOCATE_STACK must be a multiple of 8"
   %endif
   %if (%1) < 8
    %error "SEH64_ALLOCATE_STACK must have an argument that's 8 or higher."
   %elif (%1) <= 128
    SEH64_APPEND_SLOT_PAIR RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
                           SEH64_PE_ALLOC_SMALL | ((((%1) / 8) - 1) << 4)
   %elif (%1) < 512
    SEH64_APPEND_SLOT_BYTES RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
                            SEH64_PE_ALLOC_LARGE | 0, \
                            ((%1) / 8) & 0xff, ((%1) / 8) >> 8
   %else
    SEH64_APPEND_SLOT_BYTES RT_CONCAT(.seh64_op_label_,seh64_idxOps) - .start_of_prologue, \
                            SEH64_PE_ALLOC_LARGE | 1, \
                            (%1) & 0xff, ((%1) >> 8) & 0xff, ((%1) >> 16) & 0xff, ((%1) >> 24) & 0xff
   %endif
  %endif
  %assign seh64_idxOps      seh64_idxOps + 1
 %endif
%endmacro

%macro SEH64_INFO_HELPER 1
%if defined(%1)
        dw      %1
%endif
%endmacro

;;
; Ends the prologue.
%macro SEH64_END_PROLOGUE 0
.end_of_prologue:
 %ifdef RT_ASM_WITH_SEH64
        [endprolog]

 %elifdef RT_ASM_WITH_SEH64_ALT
  %ifdef ASM_FORMAT_PE
        ; Emit the unwind info now.
   %ifndef ASM_DEFINED_XDATA_SECTION
    %define ASM_DEFINED_XDATA_SECTION
        section .xdata rdata align=4
   %else
        section .xdata
        align   4, db 0
   %endif
.unwind_info:
        db      1                       ; version 1 (3 bit), no flags (5 bits)
        db      .end_of_prologue - .start_of_prologue

        db      (.unwind_info_array_end - .unwind_info_array) / 2
        db      seh64_FrameReg | (seh64_offFrame & 0xf0) ; framereg and offset/16.
.unwind_info_array:
   %ifdef seh64_slot_bytes
        db      seh64_slot_bytes
    %undef seh64_slot_bytes
   %endif
.unwind_info_array_end:

        ; Reset the segment
        BEGINCODE
  %endif
 %endif
%endmacro


;;
; Align code, pad with INT3.
%define ALIGNCODE(alignment) align alignment, db 0cch

;;
; Align data, pad with ZEROs.
%define ALIGNDATA(alignment) align alignment, db 0

;;
; Align BSS, pad with ZEROs.
%define ALIGNBSS(alignment) align alignment, resb 1

;;
; NAME_OVERLOAD can be defined by a .asm module to modify all the
; names created using the name macros in this files.
; This is handy when you've got some kind of template code.
%ifndef NAME_OVERLOAD
 %ifdef RT_MANGLER_PREFIX
  %define NAME_OVERLOAD(name) RT_MANGLER_PREFIX %+ name
 %else
  %define NAME_OVERLOAD(name) name
 %endif
%endif

;;
; Mangles the given name so it can be referenced using DECLASM() in the
; C/C++ world.
%ifndef ASM_FORMAT_BIN
 %ifdef RT_ARCH_X86
  %ifdef RT_OS_OS2
   %define NAME(name)   _ %+ NAME_OVERLOAD(name)
  %endif
  %ifdef RT_OS_WINDOWS
   %define NAME(name)   _ %+ NAME_OVERLOAD(name)
  %endif
 %endif
 %ifdef RT_OS_DARWIN
  %define NAME(name)    _ %+ NAME_OVERLOAD(name)
 %endif
%endif
%ifndef NAME
 %define NAME(name)     NAME_OVERLOAD(name)
%endif

;;
; Mangles the given C name so it will _import_ the right symbol.
%ifdef ASM_FORMAT_PE
 %define IMPNAME(name)  __imp_ %+ NAME(name)
%else
 %define IMPNAME(name)  NAME(name)
%endif

;;
; Gets the pointer to an imported object.
%ifdef ASM_FORMAT_PE
 %ifdef RT_ARCH_AMD64
  %define IMP(name)     qword [IMPNAME(name) wrt rip]
 %else
  %define IMP(name)     dword [IMPNAME(name)]
 %endif
%else
 %define IMP(name)      IMPNAME(name)
%endif

;;
; Gets the pointer to an imported object.
%ifdef ASM_FORMAT_PE
 %ifdef RT_ARCH_AMD64
  %define IMP_SEG(SegOverride, name)  qword [SegOverride:IMPNAME(name) wrt rip]
 %else
  %define IMP_SEG(SegOverride, name)  dword [SegOverride:IMPNAME(name)]
 %endif
%else
 %define IMP_SEG(SegOverride, name)   IMPNAME(name)
%endif

;;
; Declares an imported object for use with IMP2.
; @note May change the current section!
%macro EXTERN_IMP2 1
    extern IMPNAME(%1)
    BEGINDATA
 %ifdef ASM_FORMAT_MACHO
    g_Imp2_ %+ %1:  RTCCPTR_DEF IMPNAME(%1)
 %endif
%endmacro

;;
; Gets the pointer to an imported object, version 2.
%ifdef ASM_FORMAT_PE
 %ifdef RT_ARCH_AMD64
  %define IMP2(name)    qword [IMPNAME(name) wrt rip]
 %else
  %define IMP2(name)    dword [IMPNAME(name)]
 %endif
%elifdef ASM_FORMAT_ELF
 %ifdef PIC
  %ifdef RT_ARCH_AMD64
   %define IMP2(name)   qword [rel IMPNAME(name) wrt ..got]
  %else
   %define IMP2(name)   IMPNAME(name) wrt ..plt
  %endif
 %endif
%elifdef ASM_FORMAT_MACHO
 %define IMP2(name)     RTCCPTR_PRE [g_Imp2_ %+ name xWrtRIP]
%endif
%ifndef IMP2
 %define IMP2(name)     IMPNAME(name)
%endif


;;
; Define a label as given, with a '$' prepended to permit using instruction
; names like fdiv as labels.
%macro SAFE_LABEL 1
$%1:
%endmacro

;;
; Global marker which is DECLASM() compatible.
%macro GLOBALNAME 1
%ifndef ASM_FORMAT_BIN
global NAME(%1)
%endif
SAFE_LABEL NAME(%1)
%endmacro

;;
; Global exported marker which is DECLASM() compatible.
%macro EXPORTEDNAME 1
 %ifdef ASM_FORMAT_PE
  export %1=NAME(%1)
 %endif
 %ifdef __NASM__
  %ifdef ASM_FORMAT_OMF
   export NAME(%1) NAME(%1)
  %endif
%endif
GLOBALNAME %1
%endmacro

;;
; Same as GLOBALNAME_EX, but without the name mangling.
;
; @param %1     The symbol name - subjected to NAME().
; @param %2     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
;               PE ignores all but 'function' (yasm only).  Other formats ignores
;               this completely.
; @param %3     Symbol visibility: 'hidden', 'protected', 'internal', and
;                                   RT_NOTHING (for 'default' visibility).
;               These are ELF attributes, but 'hidden' is translated to
;               'private_extern' for the Macho-O format.
;               Ignored by other formats.
;
%macro GLOBALNAME_RAW 3
%ifdef ASM_FORMAT_ELF
global %1:%2 %3

%elifdef ASM_FORMAT_PE
 %ifidn %2,function
  %ifdef __YASM__      ; nasm does not support any attributes, it errors out. So, nasm is no good with control flow guard atm.
global %1:function
  %else
global %1
  %endif
 %else
global %1
 %endif

%elifdef ASM_FORMAT_MACHO
 %ifidn %3,hidden
global %1:private_extern
 %else
global %1
 %endif

%elifndef ASM_FORMAT_BIN
global %1

%endif

%1:
%endmacro

;;
; Global marker which is DECLASM() compatible.
;
; @param %1     The symbol name - subjected to NAME().
; @param %2     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
;               PE ignores all but 'function' (yasm only).  Other formats ignores
;               this completely.
; @param %3     Symbol visibility: 'hidden', 'protected', 'internal', and
;                                   RT_NOTHING (for 'default' visibility).
;               These are ELF attributes, but 'hidden' is translated to
;               'private_extern' for the Macho-O format.
;               Ignored by other formats.
;
%macro GLOBALNAME_EX 3
GLOBALNAME_RAW NAME(%1), %2, %3
%endmacro


;;
; Global exported marker, raw version w/o automatic name mangling.
;
; @param %1     The internal symbol name.
; @param %2     The external symbol name.
; @param %3     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
;               PE ignores all but 'function' (yasm only).  Other formats ignores
;               this completely.
;
%macro EXPORTEDNAME_RAW 3
 %ifdef ASM_FORMAT_PE
  export %2=%1
 %endif
 %ifdef __NASM__
  %ifdef ASM_FORMAT_OMF
   export %1 %2
  %endif
%endif
GLOBALNAME_RAW %1, %3, RT_NOTHING
%endmacro

;;
; Global exported marker which is DECLASM() compatible.
;
; @param %1     The symbol name - subjected to NAME().
; @param %2     ELF and PE attributes: 'function', 'object', 'data', 'notype'.
;               PE ignores all but 'function' (yasm only).  Other formats ignores
;               this completely.
;
%macro EXPORTEDNAME_EX 2
EXPORTEDNAME_RAW NAME(%1), %1, %2
%endmacro


;;
; Begins a procedure, raw version w/o automatic name mangling.
%macro BEGINPROC_RAW 1
 %ifdef RT_ASM_WITH_SEH64_ALT
  SEH64_ALT_START_UNWIND_INFO %1
 %endif
 %ifdef RT_ASM_WITH_SEH64
global     %1:function
proc_frame %1
 %else
GLOBALNAME_RAW %1, function, hidden
 %endif
.start_of_prologue:
%endmacro

;;
; Begins a C callable (DECLASM) procedure.
%macro BEGINPROC 1
BEGINPROC_RAW NAME(%1)
%endmacro


;;
; Begins a exported procedure, raw version w/o automatic name mangling.
; @param 1  Internal name.
; @param 2  Exported name.
%macro BEGINPROC_EXPORTED_RAW 2
 %ifdef RT_ASM_WITH_SEH64_ALT
  SEH64_ALT_START_UNWIND_INFO %1
 %endif
 %ifdef RT_ASM_WITH_SEH64
  %ifdef ASM_FORMAT_PE
export  %2=%1
  %endif
global     %1:function
proc_frame %1
 %else
EXPORTEDNAME_RAW %1, %2, function
 %endif
.start_of_prologue:
%endmacro

;;
; Begins a C callable (DECLASM) exported procedure.
%macro BEGINPROC_EXPORTED 1
BEGINPROC_EXPORTED_RAW NAME(%1), %1
%endmacro


;;
; Ends a procedure, raw version w/o automatic name mangling.
%macro ENDPROC_RAW 1
 %ifdef RT_ASM_WITH_SEH64
endproc_frame
 %endif
GLOBALNAME_RAW %1 %+ _EndProc, , hidden ; no function here, this isn't a valid code flow target.
%ifdef ASM_FORMAT_ELF
 %ifndef __NASM__ ; nasm does this in the global directive.
size %1               %1 %+ _EndProc - %1
size %1 %+ _EndProc   4 ; make it non-zero to shut up warnigns from Linux's objtool.
 %endif
%endif
    db  0xCC, 0xCC, 0xCC, 0xCC

 %ifdef RT_ASM_WITH_SEH64_ALT
  %ifdef ASM_FORMAT_PE
        ; Emit the RUNTIME_FUNCTION entry.  The linker is picky here, no label.
   %ifndef ASM_DEFINED_PDATA_SECTION
    %define ASM_DEFINED_PDATA_SECTION
        section .pdata rdata align=4
   %else
        section .pdata
   %endif
        dd      %1                    wrt ..imagebase
        dd      %1 %+ _EndProc        wrt ..imagebase
        dd      %1 %+ .unwind_info    wrt ..imagebase

        ; Restore code section.
        BEGINCODE
  %endif
 %endif
%endmacro

;;
; Ends a C callable procedure.
%macro ENDPROC 1
ENDPROC_RAW NAME(%1)
%endmacro


;
; Do OMF and Mach-O/Yasm segment definitions
;
; Both format requires this to get the segment order right, in the Mach-O/Yasm case
; it's only to make sure the .bss section ends up last (it's not declared here).
;
%ifdef ASM_FORMAT_OMF
 %ifndef RT_NOINC_SEGMENTS

  ; 16-bit segments first (OMF / OS/2 specific).
  %ifdef RT_INCL_16BIT_SEGMENTS
   segment DATA16 public CLASS=FAR_DATA align=16 use16
   segment DATA16_INIT public CLASS=FAR_DATA align=16 use16
   group DGROUP16 DATA16 DATA16_INIT

   ;;
   ; Begins 16-bit data
   %macro BEGINDATA16 0
    segment DATA16
   %endmacro

   ;;
   ; Begins 16-bit init data
   %macro BEGINDATA16INIT 0
    segment DATA16_INIT
   %endmacro

   segment CODE16 public CLASS=FAR_CODE align=16 use16
   segment CODE16_INIT public CLASS=FAR_CODE align=16 use16
   group CGROUP16 CODE16 CODE16_INIT

   ;;
   ; Begins 16-bit code
   %macro BEGINCODE16 0
    segment CODE16
   %endmacro

   ;;
   ; Begins 16-bit init code
   %macro BEGINCODE16INIT 0
    segment CODE16_INIT
   %endmacro

  %endif

  ; 32-bit segments.
  segment TEXT32 public CLASS=CODE align=16 use32 flat
  segment DATA32 public CLASS=DATA align=16 use32 flat
  segment BSS32  public CLASS=BSS  align=16 use32 flat

  ; Make the TEXT32 segment default.
  segment TEXT32
 %endif ; RT_NOINC_SEGMENTS
%endif

%ifdef ASM_FORMAT_MACHO
 %ifdef __YASM__
  section .text
  section .data
 %endif
%endif


;;
; Begins code
%ifdef ASM_FORMAT_OMF
 %macro BEGINCODE 0
  segment TEXT32
 %endmacro
%else
%macro BEGINCODE 0
 section .text
%endmacro
%endif

;;
; Begins constant (read-only) data
;
; @remarks  This is mapped to the CODE section/segment when there isn't
;           any dedicated const section/segment. (There is code that
;           assumes this, so don't try change it.)
%ifdef ASM_FORMAT_OMF
 %macro BEGINCONST 0
  segment TEXT32
 %endmacro
%else
 %macro BEGINCONST 0
  %ifdef ASM_FORMAT_MACHO ;; @todo check the other guys too.
   section .rodata
  %else
   section .text
  %endif
 %endmacro
%endif

;;
; Begins initialized data
%ifdef ASM_FORMAT_OMF
 %macro BEGINDATA 0
  segment DATA32
 %endmacro
%else
%macro BEGINDATA 0
 section .data
%endmacro
%endif

;;
; Begins uninitialized data
%ifdef ASM_FORMAT_OMF
 %macro BEGINBSS 0
  segment BSS32
 %endmacro
%else
%macro BEGINBSS 0
 section .bss
%endmacro
%endif



;; @def ARCH_BITS
; Defines the bit count of the current context.
%ifndef ARCH_BITS
 %ifdef RT_ARCH_AMD64
  %define ARCH_BITS 64
 %else
  %define ARCH_BITS 32
 %endif
%endif

;; @def HC_ARCH_BITS
; Defines the host architechture bit count.
%ifndef HC_ARCH_BITS
 %ifndef IN_RC
  %define HC_ARCH_BITS ARCH_BITS
 %else
  %define HC_ARCH_BITS 32
 %endif
%endif

;; @def R3_ARCH_BITS
; Defines the host ring-3 architechture bit count.
%ifndef R3_ARCH_BITS
 %ifdef IN_RING3
  %define R3_ARCH_BITS ARCH_BITS
 %else
  %define R3_ARCH_BITS HC_ARCH_BITS
 %endif
%endif

;; @def R0_ARCH_BITS
; Defines the host ring-0 architechture bit count.
%ifndef R0_ARCH_BITS
 %ifdef IN_RING0
  %define R0_ARCH_BITS ARCH_BITS
 %else
  %define R0_ARCH_BITS HC_ARCH_BITS
 %endif
%endif

;; @def GC_ARCH_BITS
; Defines the guest architechture bit count.
%ifndef GC_ARCH_BITS
 %ifdef IN_RC
  %define GC_ARCH_BITS ARCH_BITS
 %else
  %define GC_ARCH_BITS 32
 %endif
%endif



;; @def RTHCPTR_DEF
; The pesudo-instruction used to declare an initialized pointer variable in the host context.
%if HC_ARCH_BITS == 64
 %define RTHCPTR_DEF    dq
%else
 %define RTHCPTR_DEF    dd
%endif

;; @def RTHCPTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
; variable of the host context.
%if HC_ARCH_BITS == 64
 %define RTHCPTR_RES    resq
%else
 %define RTHCPTR_RES    resd
%endif

;; @def RTHCPTR_PRE
; The memory operand prefix used for a pointer in the host context.
%if HC_ARCH_BITS == 64
 %define RTHCPTR_PRE    qword
%else
 %define RTHCPTR_PRE    dword
%endif

;; @def RTHCPTR_CB
; The size in bytes of a pointer in the host context.
%if HC_ARCH_BITS == 64
 %define RTHCPTR_CB     8
%else
 %define RTHCPTR_CB     4
%endif



;; @def RTR0PTR_DEF
; The pesudo-instruction used to declare an initialized pointer variable in the ring-0 host context.
%if R0_ARCH_BITS == 64
 %define RTR0PTR_DEF    dq
%else
 %define RTR0PTR_DEF    dd
%endif

;; @def RTR0PTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
; variable of the ring-0 host context.
%if R0_ARCH_BITS == 64
 %define RTR0PTR_RES    resq
%else
 %define RTR0PTR_RES    resd
%endif

;; @def RTR0PTR_PRE
; The memory operand prefix used for a pointer in the ring-0 host context.
%if R0_ARCH_BITS == 64
 %define RTR0PTR_PRE    qword
%else
 %define RTR0PTR_PRE    dword
%endif

;; @def RTR0PTR_CB
; The size in bytes of a pointer in the ring-0 host context.
%if R0_ARCH_BITS == 64
 %define RTR0PTR_CB     8
%else
 %define RTR0PTR_CB     4
%endif



;; @def RTR3PTR_DEF
; The pesudo-instruction used to declare an initialized pointer variable in the ring-3 host context.
%if R3_ARCH_BITS == 64
 %define RTR3PTR_DEF    dq
%else
 %define RTR3PTR_DEF    dd
%endif

;; @def RTR3PTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
; variable of the ring-3 host context.
%if R3_ARCH_BITS == 64
 %define RTR3PTR_RES    resq
%else
 %define RTR3PTR_RES    resd
%endif

;; @def RTR3PTR_PRE
; The memory operand prefix used for a pointer in the ring-3 host context.
%if R3_ARCH_BITS == 64
 %define RTR3PTR_PRE    qword
%else
 %define RTR3PTR_PRE    dword
%endif

;; @def RTR3PTR_CB
; The size in bytes of a pointer in the ring-3 host context.
%if R3_ARCH_BITS == 64
 %define RTR3PTR_CB     8
%else
 %define RTR3PTR_CB     4
%endif



;; @def RTGCPTR_DEF
; The pesudo-instruction used to declare an initialized pointer variable in the guest context.
%if GC_ARCH_BITS == 64
 %define RTGCPTR_DEF    dq
%else
 %define RTGCPTR_DEF    dd
%endif

;; @def RTGCPTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
; variable of the guest context.
%if GC_ARCH_BITS == 64
 %define RTGCPTR_RES    resq
%else
 %define RTGCPTR_RES    resd
%endif

%define RTGCPTR32_RES   resd
%define RTGCPTR64_RES   resq

;; @def RTGCPTR_PRE
; The memory operand prefix used for a pointer in the guest context.
%if GC_ARCH_BITS == 64
 %define RTGCPTR_PRE    qword
%else
 %define RTGCPTR_PRE    dword
%endif

;; @def RTGCPTR_CB
; The size in bytes of a pointer in the guest context.
%if GC_ARCH_BITS == 64
 %define RTGCPTR_CB     8
%else
 %define RTGCPTR_CB     4
%endif


;; @def RTRCPTR_DEF
; The pesudo-instruction used to declare an initialized pointer variable in the raw mode context.
%define RTRCPTR_DEF    dd

;; @def RTRCPTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
; variable of the raw mode context.
%define RTRCPTR_RES    resd

;; @def RTRCPTR_PRE
; The memory operand prefix used for a pointer in the raw mode context.
%define RTRCPTR_PRE    dword

;; @def RTRCPTR_CB
; The size in bytes of a pointer in the raw mode context.
%define RTRCPTR_CB     4


;; @def RT_CCPTR_DEF
; The pesudo-instruction used to declare an initialized pointer variable in the current context.

;; @def RT_CCPTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized pointer
; variable of the current context.

;; @def RT_CCPTR_PRE
; The memory operand prefix used for a pointer in the current context.

;; @def RT_CCPTR_CB
; The size in bytes of a pointer in the current context.

%ifdef IN_RC
 %define RTCCPTR_DEF        RTRCPTR_DEF
 %define RTCCPTR_RES        RTRCPTR_RES
 %define RTCCPTR_PRE        RTRCPTR_PRE
 %define RTCCPTR_CB         RTRCPTR_CB
%else
 %ifdef IN_RING0
  %define RTCCPTR_DEF       RTR0PTR_DEF
  %define RTCCPTR_RES       RTR0PTR_RES
  %define RTCCPTR_PRE       RTR0PTR_PRE
  %define RTCCPTR_CB        RTR0PTR_CB
 %else
  %define RTCCPTR_DEF       RTR3PTR_DEF
  %define RTCCPTR_RES       RTR3PTR_RES
  %define RTCCPTR_PRE       RTR3PTR_PRE
  %define RTCCPTR_CB        RTR3PTR_CB
 %endif
%endif



;; @def RTHCPHYS_DEF
; The pesudo-instruction used to declare an initialized host physical address.
%define RTHCPHYS_DEF    dq

;; @def RTHCPTR_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized
; host physical address variable
%define RTHCPHYS_RES    resq

;; @def RTHCPTR_PRE
; The memory operand prefix used for a host physical address.
%define RTHCPHYS_PRE    qword

;; @def RTHCPHYS_CB
; The size in bytes of a host physical address.
%define RTHCPHYS_CB     8



;; @def RTGCPHYS_DEF
; The pesudo-instruction used to declare an initialized guest physical address.
%define RTGCPHYS_DEF    dq

;; @def RTGCPHYS_RES
; The pesudo-instruction used to declare (=reserve space for) an uninitialized
; guest physical address variable
%define RTGCPHYS_RES    resq

;; @def RTGCPTR_PRE
; The memory operand prefix used for a guest physical address.
%define RTGCPHYS_PRE    qword

;; @def RTGCPHYS_CB
; The size in bytes of a guest physical address.
%define RTGCPHYS_CB     8



;;
; The size of the long double C/C++ type.
; On 32-bit Darwin this is 16 bytes, on L4, Linux, OS/2 and Windows
; it's 12 bytes.
; @todo figure out what 64-bit Windows does (I don't recall right now).
%ifdef RT_ARCH_X86
 %ifdef RT_OS_DARWIN
  %define RTLRD_CB          16
 %else
  %define RTLRD_CB          12
 %endif
%else
  %define RTLRD_CB          16
%endif

;; @name Floating point constants along the lines of the iprt/types.h types.
; @note YASM does support special  the __Infinity__ and __NaN__ nasm tokens.
; @{
%define RTFLOAT32U_QNAN_PLUS    0x7fc00000
%define RTFLOAT32U_QNAN_MINUS   0xffc00000
%define RTFLOAT32U_INF_PLUS     0x7f800000
%define RTFLOAT32U_INF_MINUS    0xff800000

%define RTFLOAT64U_QNAN_PLUS    0x7ff8000000000000
%define RTFLOAT64U_QNAN_MINUS   0xfff8000000000000
%define RTFLOAT64U_INF_PLUS     0x7ff0000000000000
%define RTFLOAT64U_INF_MINUS    0xfff0000000000000
; @}



;; @def ASM_CALL64_GCC
; Indicates that we're using the GCC 64-bit calling convention.
; @see @ref sec_vboxrem_amd64_compare (in VBoxREMWrapper.cpp) for an ABI description.

;; @def ASM_CALL64_MSC
; Indicates that we're using the Microsoft 64-bit calling convention (fastcall on steroids).
; @see @ref sec_vboxrem_amd64_compare (in VBoxREMWrapper.cpp) for an ABI description.

; Note: On X86 we're using cdecl unconditionally. There is not yet any common
; calling convention on AMD64, that's why we need to support two different ones.)

%ifdef RT_ARCH_AMD64
 %ifndef ASM_CALL64_GCC
  %ifndef ASM_CALL64_MSC
   ; define it based on the object format.
   %ifdef ASM_FORMAT_PE
    %define ASM_CALL64_MSC
   %else
    %define ASM_CALL64_GCC
   %endif
  %endif
 %else
  ; sanity check.
  %ifdef ASM_CALL64_MSC
   %error "Only one of the ASM_CALL64_* defines should be defined!"
  %endif
 %endif
%else
 ;later; %ifdef ASM_CALL64_GCC
 ;later;  %error "ASM_CALL64_GCC is defined without RT_ARCH_AMD64!" ASM_CALL64_GCC
 ;later; %endif
 ;later; %ifdef ASM_CALL64_MSC
 ;later;  %error "ASM_CALL64_MSC is defined without RT_ARCH_AMD64!" ASM_CALL64_MSC
 ;later; %endif
%endif


;; @def RT_BEGINPROC
; Starts an IPRT procedure that should be exported unless IN_RT_STATIC is defined.
;
; @param    1       The function name.  Will apply NAME macro to it.
%macro RT_BEGINPROC 1
 %ifdef IN_RT_STATIC
BEGINPROC          %1
 %else
BEGINPROC_EXPORTED %1
 %endif
%endmacro ; RT_BEGINPROC


;; @def RT_NOCRT
; Symbol name wrapper for the No-CRT bits.
;
; In order to coexist in the same process as other CRTs, we need to
; decorate the symbols such that they don't conflict the ones in the
; other CRTs. The result of such conflicts / duplicate symbols can
; confuse the dynamic loader on unix like systems.
;
; @remark Always feed the name to this macro first and then pass the result
;         on to the next *NAME* macro.
;
%ifndef RT_WITHOUT_NOCRT_WRAPPERS
 %define RT_NOCRT(name) nocrt_ %+ name
%else
 %define RT_NOCRT(name) name
%endif

;; @def RT_NOCRT_BEGINPROC
; Starts a NOCRT procedure, taking care of name wrapping and aliasing.
;
; Weak aliases for regular crt (%1) names to the nocrt_ prefixed ones will be
; added when RT_WITH_NOCRT_ALIASES is defined and the output is ELF.  If
; RT_WITH_GENALIAS_NOCRT_ALIASES is undefined, strong aliases will be added for
; for non-ELF targets, otherwise it's assumed the genalias build tool will do
; the weak aliasing for us.
;
%macro RT_NOCRT_BEGINPROC 1
 %ifdef RT_WITH_NOCRT_ALIASES
BEGINPROC_EXPORTED RT_NOCRT(%1)
  %ifdef ASM_FORMAT_ELF
   ; ELF
   %ifdef RT_WITH_NOCRT_UNDERSCORE_ALIASES
global NAME(_ %+ %1):function
weak NAME(_ %+ %1)
SAFE_LABEL NAME(_ %+ %1)
   %endif
global NAME(%1):function
weak NAME(%1)
SAFE_LABEL NAME(%1)

  %elifndef RT_WITH_GENALIAS_NOCRT_ALIASES
   ; non-ELF when not using genalias.
   %ifdef RT_WITH_NOCRT_UNDERSCORE_ALIASES
GLOBALNAME _%1
   %endif
GLOBALNAME %1
  %endif
 %else  ; !RT_WITH_NOCRT_ALIASES
BEGINPROC_EXPORTED RT_NOCRT(%1)
 %endif ; !RT_WITH_NOCRT_ALIASES
%endmacro ; RT_NOCRT_BEGINPROC



;; @def xCB
; The stack unit size  / The register unit size.

;; @def xSP
; The stack pointer register (RSP or ESP).

;; @def xBP
; The base pointer register (RBP or ESP).

;; @def xAX
; RAX or EAX depending on context.

;; @def xBX
; RBX or EBX depending on context.

;; @def xCX
; RCX or ECX depending on context.

;; @def xDX
; RDX or EDX depending on context.

;; @def xDI
; RDI or EDI depending on context.

;; @def xSI
; RSI or ESI depending on context.

;; @def xWrtRIP
; 'wrt rip' for AMD64 targets, nothing for x86 ones.

%ifdef RT_ARCH_AMD64
 %define xCB 8
 %define xSP rsp
 %define xBP rbp
 %define xAX rax
 %define xBX rbx
 %define xCX rcx
 %define xDX rdx
 %define xDI rdi
 %define xSI rsi
 %define xWrtRIP wrt rip
%else
 %define xCB 4
 %define xSP esp
 %define xBP ebp
 %define xAX eax
 %define xBX ebx
 %define xCX ecx
 %define xDX edx
 %define xDI edi
 %define xSI esi
 %define xWrtRIP
%endif


;
; NASM sets __PASS__ to 0 in preprocess-only mode, and to 3 when only generating dependencies.
; YASM has no such setting which is why we must rely on kBuild to tell us what we're doing.
; For simplicity, we'll set the kBuild macro when using NASM.
;
%ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
 %ifdef __NASM__
  %if __PASS__ == 0 || __PASS__ == 3
   %define KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
  %endif
 %endif
%endif


;
; Some simple compile time assertions.
;
; Note! Requires new kBuild to work with YASM (see above).
;

;;
; Structure size assertion macro.
%define AssertCompileSize(a_Type, a_Size) AssertCompileSizeML a_Type, a_Size
%macro AssertCompileSizeML 2
 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
  %assign AssertVar_cbActual   %1 %+ _size
  %assign AssertVar_cbExpected %2
  %if AssertVar_cbActual != AssertVar_cbExpected
   %error %1 is AssertVar_cbActual bytes instead of AssertVar_cbExpected
  %endif
 %endif
%endmacro

;;
; Structure size alignment assertion macro.

%define AssertCompileSizeAlignment(a_Type, a_Align) AssertCompileSizeAlignmentML a_Type, a_Align
%macro AssertCompileSizeAlignmentML 2
 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
  %assign AssertVar_cbActual    %1 %+ _size
  %assign AssertVar_cbAlignment %2
  %if (AssertVar_cbActual & (AssertVar_cbAlignment - 1)) != 0
   %error %1 is AssertVar_cbActual bytes, expected size with AssertVar_cbAlignment bytes alignment.
  %endif
 %endif
%endmacro

;;
; Structure member offset assertion macro.
%define AssertCompileMemberOffset(a_Type, a_Member, a_off) AssertCompileMemberOffsetML a_Type, a_Member, a_off
%macro AssertCompileMemberOffsetML 3
 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
  %assign AssertVar_offActual   %1 %+ . %+ %2
  %assign AssertVar_offExpected %3
  %if AssertVar_offActual != AssertVar_offExpected
   %error %1 %+ . %+ %2 is at AssertVar_offActual instead of AssertVar_offExpected
  %endif
 %endif
%endmacro

;;
; Structure member alignment assertion macro.
%define AssertCompileMemberAlignment(a_Type, a_Member, a_cbAlign) AssertCompileMemberAlignmentML a_Type, a_Member, a_cbAlign
%macro AssertCompileMemberAlignmentML 3
 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
  %assign AssertVar_offActual   %1 %+ . %+ %2
  %assign AssertVar_cbAlign     %3
  %if AssertVar_offActual & (AssertVar_cbAlign - 1)
   %error %1 %+ . %+ %2 is at AssertVar_offActual, expected AssertVar_cbAlign alignment
  %endif
 %endif
%endmacro

;;
; Generic compile time expression assertion.
%define AssertCompile(a_Expr) AssertCompileML { a_Expr }
%macro AssertCompileML 1
 %ifndef KBUILD_GENERATING_MAKEFILE_DEPENDENCIES
  %if (%1) != 1
   %assign AssertVar_uResult %1
   %error %1 => AssertVar_uResult
  %endif
 %endif
%endmacro

%endif