Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
593 mikedld 6
 
7
$Revision: 1396 $
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
52
  pushf
53
  pushad
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
62
   jmp ..label
63
   ..str db _str,0
64
   ..label:
65
   mov  edx,..str
66
  else
1396 diamond 67
esp equ esp+4*8+4
358 mikedld 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 
74
     mov esi,_num
75
    else if ~_num eq esi
76
     movzx esi,_num
77
    end if
78
   else if _num eqtype 0
79
    mov esi,_num
80
   else
81
    local tp
82
    tp equ 0
83
    match [_arg],_num \{
84
     mov esi,dword[_arg]
85
     tp equ 1
86
    \}
87
    match =0 =dword[_arg],tp _num \{
88
     mov esi,dword[_arg]
89
     tp equ 1
90
    \}
91
    match =0 =word[_arg],tp _num \{
92
     movzx esi,word[_arg]
93
     tp equ 1
94
    \}
95
    match =0 =byte[_arg],tp _num \{
96
     movzx esi,byte[_arg]
97
     tp equ 1
98
    \}
99
    match =0,tp \{
100
     'Error: specified string width is incorrect'
101
    \}
102
   end if
103
  else
104
   mov esi,0x7FFFFFFF
105
  end if
106
  call fdo_debug_outstr
107
  popad
108
  popf
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 {
124
 pushf
125
 pushad
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 
140
   mov eax,_dec
141
  else if ~_dec eq eax
142
   if _sign = 1
143
    movsx eax,_dec
144
   else
145
    movzx eax,_dec
146
   end if
147
  end if
148
 else if _dec eqtype 0
149
  mov eax,_dec
150
 else
1314 diamond 151
;  add esp,4*8+4
152
esp equ esp+4*8+4
358 mikedld 153
  if _num eq
154
   mov eax,dword _dec
155
  else if _num = 1
156
   if _sign = 1
157
    movsx eax,byte _dec
158
   else
159
    movzx eax,byte _dec
160
   end if
161
  else if _num = 2
162
   if _sign = 1
163
    movsx eax,word _dec
164
   else
165
    movzx eax,word _dec
166
   end if
167
  else
168
   mov eax,dword _dec
169
  end if
1314 diamond 170
esp equ _esp
171
;  sub esp,4*8+4
358 mikedld 172
 end if
173
 mov cl,_sign
174
 call fdo_debug_outdec
175
 popad
176
 popf
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 {
192
 pushf
193
 pushad
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
200
    mov eax,_hex
201
   end if
656 mikedld 202
   mov edx,8
358 mikedld 203
  else if _hex in 
204
   if ~_hex eq ax
205
    movzx eax,_hex
206
   end if
207
   if (_num eq)
208
    mov edx,4
209
   end if
210
  else if _hex in 
211
   if ~_hex eq al
212
    movzx eax,_hex
213
   end if
214
   if (_num eq)
215
    mov edx,2
216
   end if
217
  end if
218
 else if _hex eqtype 0
219
  mov eax,_hex
220
 else
1314 diamond 221
;  add esp,4*8+4
222
esp equ esp+4*8+4
358 mikedld 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
228
  mov edx,_num
229
 else
230
  if ~_hex eqtype eax
231
   mov edx,8
232
  end if
233
 end if
234
 call fdo_debug_outhex
235
 popad
236
 popf
237
}
238
 
239
;-----------------------------------------------------------------------------
240
 
241
debug_func fdo_debug_outchar
242
debug_beginf
243
        pushad
244
        movzx   ebx,al
245
        mov     eax,1
506 mikedld 246
        mov     ecx,sys_msg_board
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
254
        mov     eax,1
255
  .l1:  dec     esi
256
        js      .l2
257
        movzx   ebx,byte[edx]
258
        or      bl,bl
259
        jz      .l2
506 mikedld 260
        mov     ecx,sys_msg_board
261
        call    ecx ; sys_msg_board
358 mikedld 262
        inc     edx
263
        jmp     .l1
264
  .l2:  ret
265
debug_endf
266
 
267
debug_func fdo_debug_outdec
268
debug_beginf
269
        or      cl,cl
270
        jz      @f
271
        or      eax,eax
272
        jns     @f
273
        neg     eax
274
        push    eax
275
        mov     al,'-'
276
        call    fdo_debug_outchar
277
        pop     eax
278
    @@: push    10
279
        pop     ecx
280
        push    -'0'
281
  .l1:  xor     edx,edx
282
        div     ecx
283
        push    edx
284
        test    eax,eax
285
        jnz     .l1
286
  .l2:  pop     eax
287
        add     al,'0'
288
        jz      .l3
289
        call    fdo_debug_outchar
290
        jmp     .l2
291
  .l3:  ret
292
debug_endf
293
 
294
debug_func fdo_debug_outhex
295
  __fdo_hexdigits db '0123456789ABCDEF'
296
debug_beginf
297
        mov     cl,dl
298
        neg     cl
299
        add     cl,8
300
        shl     cl,2
301
        rol     eax,cl
302
  .l1:  rol     eax,4
303
        push    eax
304
        and     eax,0x0000000F
305
        mov     al,[__fdo_hexdigits+eax]
306
        call    fdo_debug_outchar
307
        pop     eax
308
        dec     edx
309
        jnz     .l1
310
        ret
311
debug_endf
312
 
313
;-----------------------------------------------------------------------------
314
 
315
macro DEBUGF _level,_format,[_arg] {
316
 common
317
 if __DEBUG__ = 1 & _level >= __DEBUG_LEVEL__
318
  local ..f1,f2,a1,a2,c1,c2,c3,..lbl
319
  _debug_str_ equ __debug_str_ # a1
320
  a1 = 0
321
  c2 = 0
322
  c3 = 0
323
  f2 = 0
324
  repeat ..lbl-..f1
325
   virtual at 0
326
    db _format,0,0
327
    load c1 word from %-1
328
   end virtual
329
   if c1 = '%s'
330
    virtual at 0
331
     db _format,0,0
332
     store word 0 at %-1
333
     load c1 from f2-c2
334
    end virtual
335
    if c1 <> 0
336
     DEBUGS 0,_debug_str_+f2-c2
337
    end if
338
    c2 = c2 + 1
339
    f2 = %+1
340
    DEBUGF_HELPER S,a1,0,_arg
341
   else if c1 = '%x'
342
    virtual at 0
343
     db _format,0,0
344
     store word 0 at %-1
345
     load c1 from f2-c2
346
    end virtual
347
    if c1 <> 0
348
     DEBUGS 0,_debug_str_+f2-c2
349
    end if
350
    c2 = c2 + 1
351
    f2 = %+1
352
    DEBUGF_HELPER H,a1,0,_arg
353
   else if c1 = '%d' | c1 = '%u'
354
    local c4
355
    if c1 = '%d'
356
     c4 = 1
357
    else
358
     c4 = 0
359
    end if
360
    virtual at 0
361
     db _format,0,0
362
     store word 0 at %-1
363
     load c1 from f2-c2
364
    end virtual
365
    if c1 <> 0
366
     DEBUGS 0,_debug_str_+f2-c2
367
    end if
368
    c2 = c2 + 1
369
    f2 = %+1
370
    DEBUGF_HELPER D,a1,c4,_arg
371
   else if c1 = '\n'
372
    c3 = c3 + 1
373
   end if
374
  end repeat
375
  virtual at 0
376
   db _format,0,0
377
   load c1 from f2-c2
378
  end virtual
379
  if (c1<>0)&(f2<>..lbl-..f1-1)
380
   DEBUGS 0,_debug_str_+f2-c2
381
  end if
382
  virtual at 0
383
   ..f1 db _format,0
384
   ..lbl:
385
   __debug_strings equ __debug_strings,_debug_str_,<_format>,..lbl-..f1-1-c2-c3
386
  end virtual
387
 end if
388
}
389
 
390
macro __include_debug_strings dummy,[_id,_fmt,_len] {
391
 common
392
  local c1,a1,a2
393
 forward
394
  if defined _len & ~_len eq
395
   _id:
396
   a1 = 0
397
   a2 = 0
398
   repeat _len
399
    virtual at 0
400
     db _fmt,0,0
401
     load c1 word from %+a2-1
402
    end virtual
403
    if (c1='%s')|(c1='%x')|(c1='%d')|(c1='%u')
404
     db 0
405
     a2 = a2 + 1
406
    else if (c1='\n')
407
     dw $0A0D
408
     a1 = a1 + 1
409
     a2 = a2 + 1
410
    else
411
     db c1 and 0x0FF
412
    end if
413
   end repeat
414
   db 0
415
  end if
416
}
417
 
418
macro DEBUGF_HELPER _letter,_num,_sign,[_arg] {
419
 common
420
  local num
421
  num = 0
422
 forward
423
  if num = _num
424
   DEBUG#_letter _sign,_arg
425
  end if
426
  num = num+1
427
 common
428
  _num = _num+1
429
}
430
 
431
macro include_debug_strings {
432
 if __DEBUG__ = 1
433
  match dbg_str,__debug_strings \{
434
   __include_debug_strings dbg_str
435
  \}
436
 end if
437
}