bbdebug.h 4.0 KB

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