Subversion Repositories Kolibri OS

Rev

Rev 1161 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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