Subversion Repositories Kolibri OS

Rev

Rev 2288 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2006-2011. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. ; Macroinstructions for defining and calling procedures
  9.  
  10. macro stdcall proc,[arg]                ; directly call STDCALL procedure
  11.  { common
  12.     if ~ arg eq
  13.    reverse
  14.         pushd   arg
  15.    common
  16.     end if
  17.         call    proc }
  18.  
  19. macro invoke proc,[arg]                 ; indirectly call STDCALL procedure
  20.  { common
  21.     if ~ arg eq
  22.    reverse
  23.         pushd   arg
  24.    common
  25.     end if
  26.         call    [proc] }
  27.  
  28. macro ccall proc,[arg]                  ; directly call CDECL procedure
  29.  { common
  30.     size@ccall = 0
  31.     if ~ arg eq
  32.    reverse
  33.         pushd   arg
  34.     size@ccall = size@ccall+4
  35.    common
  36.     end if
  37.         call    proc
  38.     if size@ccall
  39.         add     esp, size@ccall
  40.     end if }
  41.  
  42. macro cinvoke proc,[arg]                ; indirectly call CDECL procedure
  43.  { common
  44.     size@ccall = 0
  45.     if ~ arg eq
  46.    reverse
  47.         pushd   arg
  48.     size@ccall = size@ccall+4
  49.    common
  50.     end if
  51.         call    [proc]
  52.     if size@ccall
  53.         add     esp, size@ccall
  54.     end if }
  55.  
  56. macro proc [args]                       ; define procedure
  57.  { common
  58.     match name params, args>
  59.     \{ define@proc name,<params \} }
  60.  
  61. prologue@proc equ prologuedef
  62.  
  63. macro prologuedef procname,flag,parmbytes,localbytes,reglist
  64.  { if parmbytes | localbytes
  65.         push    ebp
  66.         mov     ebp, esp
  67.     if localbytes
  68.         sub     esp, localbytes
  69.     end if
  70.    end if
  71.    irps reg, reglist \{ push reg \} }
  72.  
  73. epilogue@proc equ epiloguedef
  74.  
  75. macro epiloguedef procname,flag,parmbytes,localbytes,reglist
  76.  { irps reg, reglist \{ reverse pop reg \}
  77.    if parmbytes | localbytes
  78.         leave
  79.    end if
  80.    if flag and 10000b
  81.         retn
  82.    else
  83.         retn    parmbytes
  84.    end if }
  85.  
  86. macro define@proc name,statement
  87.  { local params,flag,regs,parmbytes,localbytes,current
  88.    if used name
  89.    name:
  90.    match =stdcall args, statement \{ params equ args
  91.                                      flag = 11b \}
  92.    match =stdcall, statement \{ params equ
  93.                                 flag = 11b \}
  94.    match =c args, statement \{ params equ args
  95.                                flag = 10001b \}
  96.    match =c, statement \{ params equ
  97.                           flag = 10001b \}
  98.    match =params, params \{ params equ statement
  99.                             flag = 0 \}
  100.    virtual at ebp+8
  101.    match =uses reglist=,args, params \{ regs equ reglist
  102.                                         params equ args \}
  103.    match =regs =uses reglist, regs params \{ regs equ reglist
  104.                                              params equ \}
  105.    match =regs, regs \{ regs equ \}
  106.    match =,args, params \{ defargs@proc args \}
  107.    match =args@proc args, args@proc params \{ defargs@proc args \}
  108.    parmbytes = $ - (ebp+8)
  109.    end virtual
  110.    name # % = parmbytes/4
  111.    all@vars equ
  112.    current = 0
  113.    match prologue:reglist, prologue@proc:<regs> \{ prologue name,flag,parmbytes,localbytes,reglist \}
  114.    macro locals
  115.    \{ virtual at ebp-localbytes+current
  116.       macro label . \\{ deflocal@proc .,:, \\}
  117.       struc db [val] \\{ \common deflocal@proc .,db,val \\}
  118.       struc dw [val] \\{ \common deflocal@proc .,dw,val \\}
  119.       struc dp [val] \\{ \common deflocal@proc .,dp,val \\}
  120.       struc dd [val] \\{ \common deflocal@proc .,dd,val \\}
  121.       struc dt [val] \\{ \common deflocal@proc .,dt,val \\}
  122.       struc dq [val] \\{ \common deflocal@proc .,dq,val \\}
  123.       struc rb cnt \\{ deflocal@proc .,rb cnt, \\}
  124.       struc rw cnt \\{ deflocal@proc .,rw cnt, \\}
  125.       struc rp cnt \\{ deflocal@proc .,rp cnt, \\}
  126.       struc rd cnt \\{ deflocal@proc .,rd cnt, \\}
  127.       struc rt cnt \\{ deflocal@proc .,rt cnt, \\}
  128.       struc rq cnt \\{ deflocal@proc .,rq cnt, \\} \}
  129.    macro endl
  130.    \{ purge label
  131.       restruc db,dw,dp,dd,dt,dq
  132.       restruc rb,rw,rp,rd,rt,rq
  133.       restruc byte,word,dword,pword,tword,qword
  134.       current = $-(ebp-localbytes)
  135.       end virtual \}
  136.    macro ret operand
  137.    \{ match any, operand \\{ retn operand \\}
  138.       match , operand \\{ match epilogue:reglist, epilogue@proc:<regs>
  139.                           \\\{ epilogue name,flag,parmbytes,localbytes,reglist \\\} \\} \}
  140.    macro finish@proc \{ localbytes = (((current-1) shr 2)+1) shl 2
  141.                         end if \} }
  142.  
  143. macro defargs@proc [arg]
  144.  { common
  145.     if ~ arg eq
  146.    forward
  147.      local ..arg,current@arg
  148.      match argname:type, arg
  149.       \{ current@arg equ argname
  150.          label ..arg type
  151.          argname equ ..arg
  152.          if dqword eq type
  153.            dd ?,?,?,?
  154.          else if tbyte eq type
  155.            dd ?,?,?
  156.          else if qword eq type | pword eq type
  157.            dd ?,?
  158.          else
  159.            dd ?
  160.          end if \}
  161.      match =current@arg,current@arg
  162.       \{ current@arg equ arg
  163.          arg equ ..arg
  164.          ..arg dd ? \}
  165.    common
  166.      args@proc equ current@arg
  167.    forward
  168.      restore current@arg
  169.    common
  170.     end if }
  171.  
  172. macro deflocal@proc name,def,[val]
  173.  { common
  174.     match vars, all@vars \{ all@vars equ all@vars, \}
  175.     all@vars equ all@vars name
  176.    forward
  177.     local ..var,..tmp
  178.     ..var def val
  179.     match =?, val \{ ..tmp equ \}
  180.     match any =dup (=?), val \{ ..tmp equ \}
  181.     match tmp : value, ..tmp : val
  182.      \{ tmp: end virtual
  183.         initlocal@proc ..var,def value
  184.         virtual at tmp\}
  185.    common
  186.     match first rest, ..var, \{ name equ first \} }
  187.  
  188. macro initlocal@proc name,def
  189.  { virtual at name
  190.     def
  191.     size@initlocal = $ - name
  192.    end virtual
  193.    position@initlocal = 0
  194.    while size@initlocal > position@initlocal
  195.     virtual at name
  196.      def
  197.      if size@initlocal - position@initlocal < 2
  198.       current@initlocal = 1
  199.       load byte@initlocal byte from name+position@initlocal
  200.      else if size@initlocal - position@initlocal < 4
  201.       current@initlocal = 2
  202.       load word@initlocal word from name+position@initlocal
  203.      else
  204.       current@initlocal = 4
  205.       load dword@initlocal dword from name+position@initlocal
  206.      end if
  207.     end virtual
  208.     if current@initlocal = 1
  209.         mov     byte [name+position@initlocal], byte@initlocal
  210.     else if current@initlocal = 2
  211.         mov     word [name+position@initlocal], word@initlocal
  212.     else
  213.         mov     dword [name+position@initlocal], dword@initlocal
  214.     end if
  215.     position@initlocal = position@initlocal + current@initlocal
  216.    end while }
  217.  
  218. macro endp
  219.  { purge ret,locals,endl
  220.    finish@proc
  221.    purge finish@proc
  222.    restore regs@proc
  223.    match all,args@proc \{ restore all \}
  224.    restore args@proc
  225.    match all,all@vars \{ restore all \} }
  226.  
  227. macro local [var]
  228.  { common
  229.     locals
  230.    forward done@local equ
  231.     match varname[count]:vartype, var
  232.     \{ match =BYTE, vartype \\{ varname rb count
  233.                                 restore done@local \\}
  234.        match =WORD, vartype \\{ varname rw count
  235.                                 restore done@local \\}
  236.        match =DWORD, vartype \\{ varname rd count
  237.                                  restore done@local \\}
  238.        match =PWORD, vartype \\{ varname rp count
  239.                                  restore done@local \\}
  240.        match =QWORD, vartype \\{ varname rq count
  241.                                  restore done@local \\}
  242.        match =TBYTE, vartype \\{ varname rt count
  243.                                  restore done@local \\}
  244.        match =DQWORD, vartype \\{ label varname dqword
  245.                                   rq count+count
  246.                                   restore done@local \\}
  247.        match , done@local \\{ virtual
  248.                                varname vartype
  249.                               end virtual
  250.                               rb count*sizeof.\#vartype
  251.                               restore done@local \\} \}
  252.     match :varname:vartype, done@local:var
  253.     \{ match =BYTE, vartype \\{ varname db ?
  254.                                 restore done@local \\}
  255.        match =WORD, vartype \\{ varname dw ?
  256.                                 restore done@local \\}
  257.        match =DWORD, vartype \\{ varname dd ?
  258.                                  restore done@local \\}
  259.        match =PWORD, vartype \\{ varname dp ?
  260.                                  restore done@local \\}
  261.        match =QWORD, vartype \\{ varname dq ?
  262.                                  restore done@local \\}
  263.        match =TBYTE, vartype \\{ varname dt ?
  264.                                  restore done@local \\}
  265.        match =DQWORD, vartype \\{ label varname dqword
  266.                                   dq ?,?
  267.                                   restore done@local \\}
  268.        match , done@local \\{ varname vartype
  269.                               restore done@local \\} \}
  270.     match ,done@local
  271.     \{ var
  272.        restore done@local \}
  273.    common
  274.     endl }
  275.