bbdebug.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef BB_DEBUG_H
  2. #define BB_DEBUG_H
  3. #include "bbstring.h"
  4. struct bbDBFiber;
  5. struct bbDBFrame;
  6. struct bbDBVarType;
  7. struct bbDBVar;
  8. inline bbString bbDBType( const void *p ){ return "Void"; }
  9. inline bbString bbDBType( bbBool *p ){ return "Bool"; }
  10. inline bbString bbDBType( bbByte *p ){ return "Byte"; }
  11. inline bbString bbDBType( bbUByte *p ){ return "UByte"; }
  12. inline bbString bbDBType( bbShort *p ){ return "Short"; }
  13. inline bbString bbDBType( bbUShort *p ){ return "UShort"; }
  14. inline bbString bbDBType( bbInt *p ){ return "Int"; }
  15. inline bbString bbDBType( bbUInt *p ){ return "UInt"; }
  16. inline bbString bbDBType( bbLong *p ){ return "Long"; }
  17. inline bbString bbDBType( bbULong *p ){ return "ULong"; }
  18. inline bbString bbDBType( bbFloat *p ){ return "Float"; }
  19. inline bbString bbDBType( bbDouble *p ){ return "Double"; }
  20. inline bbString bbDBType( bbString *p ){ return "String"; }
  21. inline bbString bbDBValue( void *p ){ return "?????"; }
  22. inline bbString bbDBValue( bbBool *p ){ return *p ? "True" : "False"; }
  23. inline bbString bbDBValue( bbByte *p ){ return bbString(*p); }
  24. inline bbString bbDBValue( bbUByte *p ){ return bbString(*p); }
  25. inline bbString bbDBValue( bbShort *p ){ return bbString(*p); }
  26. inline bbString bbDBValue( bbUShort *p ){ return bbString(*p); }
  27. inline bbString bbDBValue( bbInt *p ){ return bbString(*p); }
  28. inline bbString bbDBValue( bbUInt *p ){ return bbString(*p); }
  29. inline bbString bbDBValue( bbLong *p ){ return bbString(*p); }
  30. inline bbString bbDBValue( bbULong *p ){ return bbString(*p); }
  31. inline bbString bbDBValue( bbFloat *p ){ return bbString(*p); }
  32. inline bbString bbDBValue( bbDouble *p ){ return bbString(*p); }
  33. extern bbString bbDBValue( bbString *p );
  34. template<class T> bbString bbDBType(){
  35. return bbDBType( (T*)0 );
  36. }
  37. template<class T> bbString bbDBType( bbGCVar<T> *p ){ return bbDBType<T*>(); }
  38. template<class T> bbString bbDBValue( bbGCVar<T> *p ){ T *t=p->get();return t ? bbDBValue( &t ) : "Null"; }
  39. template<class T> bbString bbDBType( T **p ){ return bbDBType<T>()+" Ptr"; }
  40. template<class T> bbString bbDBValue( T **p ){ char buf[64];sprintf( buf,"$%x",p );return buf; }
  41. struct bbDBVarType{
  42. virtual bbString type()=0;
  43. virtual bbString value( void *p )=0;
  44. };
  45. template<class T> struct bbDBVarType_t : public bbDBVarType{
  46. bbString type(){
  47. return bbDBType( (T*)0 );
  48. }
  49. bbString value( void *p ){
  50. return bbDBValue( (T*)p );
  51. }
  52. static bbDBVarType_t info;
  53. };
  54. template<class T> bbDBVarType_t<T> bbDBVarType_t<T>::info;
  55. struct bbDBVar{
  56. const char *name;
  57. bbDBVarType *type;
  58. void *var;
  59. };
  60. struct bbDBContext{
  61. bbDBFrame *frames=nullptr;
  62. bbDBVar *localsBuf=nullptr;
  63. bbDBVar *locals=nullptr;
  64. int stopped;
  65. ~bbDBContext();
  66. void init();
  67. };
  68. namespace bbDB{
  69. extern int nextSeq;
  70. extern bbDBContext *currentContext;
  71. void init();
  72. void stop();
  73. void stopped();
  74. void error( bbString err );
  75. bbArray<bbString> stack();
  76. void emitStack();
  77. }
  78. struct bbDBFrame{
  79. bbDBFrame *succ;
  80. bbDBVar *locals;
  81. const char *decl;
  82. const char *srcFile;
  83. int srcPos;
  84. int seq;
  85. bbDBFrame( const char *decl,const char *srcFile ):succ( bbDB::currentContext->frames ),locals( bbDB::currentContext->locals ),decl( decl ),srcFile( srcFile ){
  86. bbDB::currentContext->frames=this;
  87. --bbDB::currentContext->stopped;
  88. seq=++bbDB::nextSeq;
  89. }
  90. ~bbDBFrame(){
  91. ++bbDB::nextSeq;
  92. ++bbDB::currentContext->stopped;
  93. bbDB::currentContext->locals=locals;
  94. bbDB::currentContext->frames=succ;
  95. }
  96. };
  97. struct bbDBBlock{
  98. bbDBVar *locals;
  99. bbDBBlock():locals( bbDB::currentContext->locals ){
  100. }
  101. ~bbDBBlock(){
  102. bbDB::currentContext->locals=locals;
  103. }
  104. };
  105. struct bbDBLoop : public bbDBBlock{
  106. bbDBLoop(){
  107. --bbDB::currentContext->stopped;
  108. }
  109. ~bbDBLoop(){
  110. ++bbDB::currentContext->stopped;
  111. }
  112. };
  113. inline void bbDBStmt( int srcPos ){
  114. bbDB::currentContext->frames->srcPos=srcPos;
  115. if( bbDB::currentContext->stopped>=0 ) bbDB::stopped();
  116. }
  117. template<class T> void bbDBEmit( const char *name,T *var ){
  118. bbDBVarType *type=&bbDBVarType_t<T>::info;
  119. puts( (BB_T( name )+":"+type->type()+"="+type->value( var )).c_str() );
  120. }
  121. template<class T> void bbDBEmit( const char *name,bbGCVar<T> *p ){
  122. T *var=p->get();return bbDBEmit( name,&var );
  123. }
  124. template<class T> void bbDBLocal ( const char *name,T *var ){
  125. bbDB::currentContext->locals->name=name;
  126. bbDB::currentContext->locals->type=&bbDBVarType_t<T>::info;
  127. bbDB::currentContext->locals->var=var;
  128. ++bbDB::currentContext->locals;
  129. }
  130. #endif