Subversion Repositories Kolibri OS

Rev

Rev 431 | Rev 593 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. $Revision: 465 $
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;                                                              ;;
  4. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  5. ;; Distributed under terms of the GNU General Public License    ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7. ;
  8. ; Formatted Debug Output (FDO)
  9. ; Copyright (c) 2005-2006, mike.dld
  10. ; Created: 2005-01-29, Changed: 2006-11-10
  11. ;
  12. ; For questions and bug reports, mail to mike.dld@gmail.com
  13. ;
  14. ; Available format specifiers are: %s, %d, %u, %x (with partial width support)
  15. ;
  16.  
  17. ; to be defined:
  18. ;   __DEBUG__ equ 1
  19. ;   __DEBUG_LEVEL__ equ 5
  20.  
  21. macro debug_func name {
  22.  if used name
  23.   name@of@func equ name
  24. }
  25.  
  26. macro debug_beginf {
  27.  align 4
  28.  name@of@func:
  29. }
  30.  
  31. debug_endf fix end if
  32.  
  33. macro DEBUGS _sign,[_str] {
  34.  common
  35.   local tp
  36.   tp equ 0
  37.   match _arg:_num,_str \{
  38.    DEBUGS_N _sign,_num,_arg
  39.    tp equ 1
  40.   \}
  41.   match =0 _arg,tp _str \{
  42.    DEBUGS_N _sign,,_arg
  43.   \}
  44. }
  45.  
  46. macro DEBUGS_N _sign,_num,[_str] {
  47.  common
  48.   pushf
  49.   pushad
  50.   local ..str,..label,is_str
  51.   is_str = 0
  52.  forward
  53.   if _str eqtype ''
  54.    is_str = 1
  55.   end if
  56.  common
  57.   if is_str = 1
  58.    jmp ..label
  59.    ..str db _str,0
  60.    ..label:
  61.    add  esp,4*8+4
  62.    mov  edx,..str
  63.    sub  esp,4*8+4
  64.   else
  65.    mov  edx,_str
  66.   end if
  67.   if ~_num eq
  68.    if _num eqtype eax
  69.     if _num in <eax,ebx,ecx,edx,edi,ebp,esp>
  70.      mov esi,_num
  71.     else if ~_num eq esi
  72.      movzx esi,_num
  73.     end if
  74.    else if _num eqtype 0
  75.     mov esi,_num
  76.    else
  77.     local tp
  78.     tp equ 0
  79.     match [_arg],_num \{
  80.      mov esi,dword[_arg]
  81.      tp equ 1
  82.     \}
  83.     match =0 =dword[_arg],tp _num \{
  84.      mov esi,dword[_arg]
  85.      tp equ 1
  86.     \}
  87.     match =0 =word[_arg],tp _num \{
  88.      movzx esi,word[_arg]
  89.      tp equ 1
  90.     \}
  91.     match =0 =byte[_arg],tp _num \{
  92.      movzx esi,byte[_arg]
  93.      tp equ 1
  94.     \}
  95.     match =0,tp \{
  96.      'Error: specified string width is incorrect'
  97.     \}
  98.    end if
  99.   else
  100.    mov esi,0x7FFFFFFF
  101.   end if
  102.   call fdo_debug_outstr
  103.   popad
  104.   popf
  105. }
  106.  
  107. macro DEBUGD _sign,_dec {
  108.  local tp
  109.  tp equ 0
  110.  match _arg:_num,_dec \{
  111.   DEBUGD_N _sign,_num,_arg
  112.   tp equ 1
  113.  \}
  114.  match =0 _arg,tp _dec \{
  115.   DEBUGD_N _sign,,_arg
  116.  \}
  117. }
  118.  
  119. macro DEBUGD_N _sign,_num,_dec {
  120.  pushf
  121.  pushad
  122.  if (~_num eq)
  123.   if (_dec eqtype eax | _dec eqtype 0)
  124.    'Error: precision allowed only for in-memory variables'
  125.   end if
  126.   if (~_num in <1,2,4>)
  127.    if _sign
  128.     'Error: 1, 2 and 4 are only allowed for precision in %d'
  129.    else
  130.     'Error: 1, 2 and 4 are only allowed for precision in %u'
  131.    end if
  132.   end if
  133.  end if
  134.  if _dec eqtype eax
  135.   if _dec in <ebx,ecx,edx,esi,edi,ebp,esp>
  136.    mov eax,_dec
  137.   else if ~_dec eq eax
  138.    if _sign = 1
  139.     movsx eax,_dec
  140.    else
  141.     movzx eax,_dec
  142.    end if
  143.   end if
  144.  else if _dec eqtype 0
  145.   mov eax,_dec
  146.  else
  147.   add esp,4*8+4
  148.   if _num eq
  149.    mov eax,dword _dec
  150.   else if _num = 1
  151.    if _sign = 1
  152.     movsx eax,byte _dec
  153.    else
  154.     movzx eax,byte _dec
  155.    end if
  156.   else if _num = 2
  157.    if _sign = 1
  158.     movsx eax,word _dec
  159.    else
  160.     movzx eax,word _dec
  161.    end if
  162.   else
  163.    mov eax,dword _dec
  164.   end if
  165.   sub esp,4*8+4
  166.  end if
  167.  mov cl,_sign
  168.  call fdo_debug_outdec
  169.  popad
  170.  popf
  171. }
  172.  
  173. macro DEBUGH _sign,_hex {
  174.  local tp
  175.  tp equ 0
  176.  match _arg:_num,_hex \{
  177.   DEBUGH_N _sign,_num,_arg
  178.   tp equ 1
  179.  \}
  180.  match =0 _arg,tp _hex \{
  181.   DEBUGH_N _sign,,_arg
  182.  \}
  183. }
  184.  
  185. macro DEBUGH_N _sign,_num,_hex {
  186.  pushf
  187.  pushad
  188.  if (~_num eq) & (~_num in <1,2,3,4,5,6,7,8>)
  189.   'Error: 1..8 are only allowed for precision in %x'
  190.  end if
  191.  if _hex eqtype eax
  192.   if _hex in <eax,ebx,ecx,edx,esi,edi,ebp,esp>
  193.    if ~_hex eq eax
  194.     mov eax,_hex
  195.    end if
  196.   else if _hex in <ax,bx,cx,dx,si,di,bp,sp>
  197.    if ~_hex eq ax
  198.     movzx eax,_hex
  199.    end if
  200.    shl eax,16
  201.    if (_num eq)
  202.     mov edx,4
  203.    end if
  204.   else if _hex in <al,ah,bl,bh,cl,ch,dl,dh>
  205.    if ~_hex eq al
  206.     movzx eax,_hex
  207.    end if
  208.    shl eax,24
  209.    if (_num eq)
  210.     mov edx,2
  211.    end if
  212.   end if
  213.  else if _hex eqtype 0
  214.   mov eax,_hex
  215.  else
  216.   add esp,4*8+4
  217.   mov eax,dword _hex
  218.   sub esp,4*8+4
  219.  end if
  220.  if ~_num eq
  221.   mov edx,_num
  222.  else
  223.   if ~_hex eqtype eax
  224.    mov edx,8
  225.   end if
  226.  end if
  227.  call fdo_debug_outhex
  228.  popad
  229.  popf
  230. }
  231.  
  232. ;-----------------------------------------------------------------------------
  233.  
  234. debug_func fdo_debug_outchar
  235. debug_beginf
  236.         pushad
  237.         movzx   ebx,al
  238.         mov     eax,1
  239.      ;   call    sys_msg_board
  240.         popad
  241.         ret
  242. debug_endf
  243.  
  244. debug_func fdo_debug_outstr
  245. debug_beginf
  246.         mov     eax,1
  247.   .l1:  dec     esi
  248.         js      .l2
  249.         movzx   ebx,byte[edx]
  250.         or      bl,bl
  251.         jz      .l2
  252.      ;   call    sys_msg_board
  253.         inc     edx
  254.         jmp     .l1
  255.   .l2:  ret
  256. debug_endf
  257.  
  258. debug_func fdo_debug_outdec
  259. debug_beginf
  260.         or      cl,cl
  261.         jz      @f
  262.         or      eax,eax
  263.         jns     @f
  264.         neg     eax
  265.         push    eax
  266.         mov     al,'-'
  267.         call    fdo_debug_outchar
  268.         pop     eax
  269.     @@: push    10
  270.         pop     ecx
  271.         push    -'0'
  272.   .l1:  xor     edx,edx
  273.         div     ecx
  274.         push    edx
  275.         test    eax,eax
  276.         jnz     .l1
  277.   .l2:  pop     eax
  278.         add     al,'0'
  279.         jz      .l3
  280.         call    fdo_debug_outchar
  281.         jmp     .l2
  282.   .l3:  ret
  283. debug_endf
  284.  
  285. debug_func fdo_debug_outhex
  286.   __fdo_hexdigits db '0123456789ABCDEF'
  287. debug_beginf
  288.         mov     cl,dl
  289.         neg     cl
  290.         add     cl,8
  291.         shl     cl,2
  292.         rol     eax,cl
  293.   .l1:  rol     eax,4
  294.         push    eax
  295.         and     eax,0x0000000F
  296.         mov     al,[__fdo_hexdigits+eax]
  297.         call    fdo_debug_outchar
  298.         pop     eax
  299.         dec     edx
  300.         jnz     .l1
  301.         ret
  302. debug_endf
  303.  
  304. ;-----------------------------------------------------------------------------
  305.  
  306. macro DEBUGF _level,_format,[_arg] {
  307.  common
  308.  if __DEBUG__ = 1 & _level >= __DEBUG_LEVEL__
  309.   local ..f1,f2,a1,a2,c1,c2,c3,..lbl
  310.   _debug_str_ equ __debug_str_ # a1
  311.   a1 = 0
  312.   c2 = 0
  313.   c3 = 0
  314.   f2 = 0
  315.   repeat ..lbl-..f1
  316.    virtual at 0
  317.     db _format,0,0
  318.     load c1 word from %-1
  319.    end virtual
  320.    if c1 = '%s'
  321.     virtual at 0
  322.      db _format,0,0
  323.      store word 0 at %-1
  324.      load c1 from f2-c2
  325.     end virtual
  326.     if c1 <> 0
  327.      DEBUGS 0,_debug_str_+f2-c2
  328.     end if
  329.     c2 = c2 + 1
  330.     f2 = %+1
  331.     DEBUGF_HELPER S,a1,0,_arg
  332.    else if c1 = '%x'
  333.     virtual at 0
  334.      db _format,0,0
  335.      store word 0 at %-1
  336.      load c1 from f2-c2
  337.     end virtual
  338.     if c1 <> 0
  339.      DEBUGS 0,_debug_str_+f2-c2
  340.     end if
  341.     c2 = c2 + 1
  342.     f2 = %+1
  343.     DEBUGF_HELPER H,a1,0,_arg
  344.    else if c1 = '%d' | c1 = '%u'
  345.     local c4
  346.     if c1 = '%d'
  347.      c4 = 1
  348.     else
  349.      c4 = 0
  350.     end if
  351.     virtual at 0
  352.      db _format,0,0
  353.      store word 0 at %-1
  354.      load c1 from f2-c2
  355.     end virtual
  356.     if c1 <> 0
  357.      DEBUGS 0,_debug_str_+f2-c2
  358.     end if
  359.     c2 = c2 + 1
  360.     f2 = %+1
  361.     DEBUGF_HELPER D,a1,c4,_arg
  362.    else if c1 = '\n'
  363.     c3 = c3 + 1
  364.    end if
  365.   end repeat
  366.   virtual at 0
  367.    db _format,0,0
  368.    load c1 from f2-c2
  369.   end virtual
  370.   if (c1<>0)&(f2<>..lbl-..f1-1)
  371.    DEBUGS 0,_debug_str_+f2-c2
  372.   end if
  373.   virtual at 0
  374.    ..f1 db _format,0
  375.    ..lbl:
  376.    __debug_strings equ __debug_strings,_debug_str_,<_format>,..lbl-..f1-1-c2-c3
  377.   end virtual
  378.  end if
  379. }
  380.  
  381. macro __include_debug_strings dummy,[_id,_fmt,_len] {
  382.  common
  383.   local c1,a1,a2
  384.  forward
  385.   if defined _len & ~_len eq
  386.    _id:
  387.    a1 = 0
  388.    a2 = 0
  389.    repeat _len
  390.     virtual at 0
  391.      db _fmt,0,0
  392.      load c1 word from %+a2-1
  393.     end virtual
  394.     if (c1='%s')|(c1='%x')|(c1='%d')|(c1='%u')
  395.      db 0
  396.      a2 = a2 + 1
  397.     else if (c1='\n')
  398.      dw $0A0D
  399.      a1 = a1 + 1
  400.      a2 = a2 + 1
  401.     else
  402.      db c1 and 0x0FF
  403.     end if
  404.    end repeat
  405.    db 0
  406.   end if
  407. }
  408.  
  409. macro DEBUGF_HELPER _letter,_num,_sign,[_arg] {
  410.  common
  411.   local num
  412.   num = 0
  413.  forward
  414.   if num = _num
  415.    DEBUG#_letter _sign,_arg
  416.   end if
  417.   num = num+1
  418.  common
  419.   _num = _num+1
  420. }
  421.  
  422. macro include_debug_strings {
  423.  if __DEBUG__ = 1
  424.   match dbg_str,__debug_strings \{
  425.    __include_debug_strings dbg_str
  426.   \}
  427.  end if
  428. }
  429.