home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / callconv.inc < prev    next >
Text File  |  1998-04-25  |  7KB  |  304 lines

  1. ;****************************CallConv.Inc************************************
  2. ;
  3. ;   Copyright (c) 1990-1997, Microsoft Corp. All rights reserved. 
  4. ;
  5. ;****************************************************************************
  6.  
  7. ;****************************Public Macro************************************
  8. ;
  9. ;   ComposeInst Inst,p1,p2,p3,p4,p5,p6,p7,p8,p9
  10. ;
  11. ;       This macro simply concatenates all arguments into one string.
  12. ;
  13. ;
  14. ;****************************************************************************
  15.  
  16. ComposeInst macro   Inst,p1,p2,p3,p4,p5,p6,p7,p8,p9
  17.         &Inst   p1&p2&p3&p4&p5&p6&p7&p8&p9
  18. endm
  19.  
  20. ;****************************Public Macro************************************
  21. ;
  22. ;   CountArg    cCount,ArgList
  23. ;
  24. ;       This macro count the number of arguments in the ArgList and returns
  25. ;       the value in cCount.
  26. ;
  27. ;
  28. ;****************************************************************************
  29.  
  30. CountArg    macro   cCount,ArgList
  31.  
  32.         cCount = 0
  33.  
  34.         irp arg,<ArgList>
  35.             cCount = cCount+1
  36.         endm
  37. endm
  38.  
  39. ;****************************Public Macro************************************
  40. ;
  41. ;   RevPush     ArgList,cCount
  42. ;
  43. ;       This macro pushes the arguments in ArgList in the reverse order
  44. ;       and returns the number of arguments in cCount.
  45. ;
  46. ;
  47. ;****************************************************************************
  48.  
  49. RevPush macro   ArgList,cCount
  50.         Local   index,x
  51.  
  52.         CountArg cCount,<ArgList>
  53.  
  54.         index  = cCount
  55.         rept    cCount
  56.             x = 0
  57.             irp arg,<ArgList>
  58.                 x = x+1
  59.                 ife index-x
  60.                     push    arg
  61.                     exitm
  62.                 endif
  63.             endm
  64.             index = index-1
  65.         endm
  66. endm
  67.  
  68. ;****************************Public Macro************************************
  69. ;
  70. ;   The following sections contain calling-convention related macros for:
  71. ;
  72. ;   PUBLICP     Func,N
  73. ;       to define a public label
  74. ;
  75. ;   EXTRNP      Func,N,Thunk
  76. ;       to define a external near label
  77. ;
  78. ;   LABELP      Func,N
  79. ;       to label an address as a routine entry point
  80. ;
  81. ;   stdPROC       Func,N,ArgList
  82. ;       to declare a routine header
  83. ;
  84. ;   ProcName    Name,Func,N
  85. ;       to rename a function Func to Name. Using it in conjunction with
  86. ;       normal function declaration (with the new name) will solve an error
  87. ;       caused by a long parameter list routine that exhausts page width.
  88. ;
  89. ;   stdRET        Func
  90. ;       to return from Func routines (declared with stdPROC or ProcName.)
  91. ;
  92. ;   stdENDP     Func
  93. ;       to declare the end of routine (declared with stdPROC or ProcName.)
  94. ;
  95. ;   endMod      Func
  96. ;       to declare the end of module with an entry point at Func (declared
  97. ;       with stdPROC or ProcName.)
  98. ;
  99. ;   stdCall     Func,ArgList
  100. ;       to call to a routine--Func--with the arguments pushed on the stack
  101. ;
  102. ;   MovAddr     dest,Func,n
  103. ;       to move the address of the routine--Func--into dest.
  104. ;
  105. ;   Note that for the standard calling convention all the function names,
  106. ;   Func, are automatically converted to Func@N where N is the number of
  107. ;   bytes (decimal) in the argument list.
  108. ;
  109. ;
  110. ;****************************************************************************
  111.  
  112. if      @Version GE 600
  113.         option  nokeyword:<stdcall>
  114. endif
  115.  
  116. PUBLICP macro   Func,N
  117.  
  118.         ifb    <N>
  119.             public      Func&@0
  120.         else
  121.             PUBLICP2    Func,%(N*4)
  122.         endif
  123. endm
  124.  
  125. PUBLICP2 macro   Func,N
  126.  
  127.         public  Func&@&N
  128. endm
  129.  
  130. EXTRNP  macro   Func,N,Thunk,FastCall
  131.         ifb    <N>
  132.             IFNDEF  Func&@0
  133.                 extrn       Func&@0:NEAR
  134.             ENDIF
  135.         else
  136.             ifb     <FastCall>
  137.                 ifb     <Thunk>
  138.                     EXTRNP2     Func,%(N*4)
  139.                 else
  140.                     EXTRNTHUNK  Func,%(N*4)
  141.                 endif
  142.             else
  143.                 cFCall&@&Func equ   (N*4)
  144.                 ifb     <Thunk>
  145.                     EXTRNP2     &@&Func,%(N*4)
  146.                 else
  147.                     EXTRNTHUNK  &@&Func,%(N*4)
  148.                 endif
  149.             endif
  150.         endif
  151. endm
  152.  
  153. EXTRNP2 macro   Func,N
  154.         IFNDEF  Func&@&N
  155.             extrn   Func&@&N:NEAR
  156.         ENDIF
  157. endm
  158.  
  159. EXTRNTHUNK macro   Func,N
  160.         IFNDEF  __imp_&Func&@&N
  161.             extrn       __imp_&Func&@&N:DWORD
  162.         ENDIF
  163. endm
  164.  
  165. LABELP  macro   Func,N
  166.  
  167.         ifb    <N>
  168.             Func&@0 label   near
  169.         else
  170.             LABELP2 Func,%(N*4)
  171.         endif
  172. endm
  173.  
  174. LABELP2 macro   Func,N
  175.  
  176. Func&@&N    label   near
  177.  
  178. endm
  179.  
  180. ProcName macro  Name,Func,N
  181.  
  182.         ifb <N>
  183.             cByte&Func  equ     0
  184.             Name        equ     <Func&@0>
  185.         else
  186.             cByte&Func  equ     N
  187.             Name        equ     <Func&@&N>
  188.         endif
  189. endm
  190.  
  191. stdPROC   macro   Func,N,ArgList
  192.  
  193.         ProcName    Func,Func,%(N*4)
  194.  
  195.         Func        proc    ArgList
  196. endm
  197.  
  198. cPublicProc macro Func,N,ArgList
  199.         align   dword
  200.         PUBLICP Func,N
  201.         ifb <N>
  202.             stdPROC Func,0,<ArgList>
  203.         else
  204.             stdPROC Func,N,<ArgList>
  205.         endif
  206. endm
  207.  
  208. ProcNameF macro  Name,Func,N,M
  209.  
  210.         cByte&Func  equ     M
  211.         cFCall&Func equ     N
  212.         Name        equ     <Func&@&N>
  213.  
  214. endm
  215.  
  216. stdPROCF  macro   Func,N,ArgList
  217.  
  218.         if N gt 2
  219.             ProcNameF   Func,Func,%(N*4),%((N-2)*4)
  220.         else
  221.             ProcNameF   Func,Func,%(N*4),0
  222.         endif
  223.  
  224.         Func        proc    ArgList
  225. endm
  226.  
  227. cPublicFastCall macro Func,N,ArgList
  228.         align   dword
  229.         PUBLICP &@&Func,N
  230.         ifb <N>
  231.             stdPROCF &@&Func,0,<ArgList>
  232.         else
  233.             stdPROCF &@&Func,N,<ArgList>
  234.         endif
  235. endm
  236.  
  237. fstRET  macro   Func
  238.         ret     cByte&@&Func
  239. endm
  240.  
  241. stdRET  macro   Func
  242.         ret     cByte&Func
  243. endm
  244.  
  245. cPublicFpo macro FpoLocals, FpoParams
  246.  
  247. .FPO ( FpoParams, FpoLocals, 0, 0, 0, 0 )
  248.  
  249. endm
  250.  
  251.  
  252. fstENDP macro   Func
  253.  
  254.         &@&Func    endp
  255. endm
  256.  
  257. stdENDP macro   Func
  258.  
  259.         Func    endp
  260. endm
  261.  
  262. endMod  macro   Func
  263.  
  264.         end     Func
  265. endm
  266.  
  267. stdCallCall macro  Func,N
  268.     IFDEF   __imp_&Func&@&N
  269.         call    dword ptr [__imp_&Func&@&N]
  270.     ELSE
  271.         call    Func&@&N
  272.     ENDIF
  273. endm
  274.  
  275.  
  276. stdCall macro   Func,ArgList
  277.         Local   Bytes
  278.  
  279.         RevPush <ArgList>,Bytes
  280.         Bytes = Bytes*4
  281.  
  282.         stdCallCall   Func,%(Bytes)
  283. endm
  284.  
  285. fstCall macro   Func,ArgList
  286.         Local   Bytes
  287.  
  288.         RevPush <ArgList>,Bytes
  289.         Bytes = Bytes*4
  290.  
  291.         if Bytes eq 0
  292.             stdCallCall   &@&Func,%cFCall&@&Func
  293.         else
  294.             ; must have 2 register params
  295.             stdCallCall   &@&Func,%(Bytes+8)
  296.         endif
  297. endm
  298.  
  299.  
  300. MovAddr macro   dest,addr,n
  301.  
  302.         ComposeInst <mov >,dest,<,offset FLAT:>,addr,<@>,n
  303. endm
  304.