Subversion Repositories Kolibri OS

Rev

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

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