Subversion Repositories Kolibri OS

Rev

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

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