Browse Source

More work on debug system.

Mark Sibly 9 years ago
parent
commit
002b3e2224

+ 6 - 8
modules/monkey/native/bbarray.h

@@ -172,14 +172,12 @@ template<class T,int D> class bbArray : public bbGCNode{
 	}
 };
 
-template<class T,int N> bbDBType *bbDBTypeOf( bbArray<T,N>** ){
-	struct type : public bbDBType{
-		bbDBType *elemType=bbDBTypeOf<T>();
-		int rank=N;
-		bbString name(){ return elemType->name()+BB_T("[]"); }
-	};
-	static type _type;
-	return &_type;
+template<class T,int N> bbString bbDBType( bbArray<T,N> **p ){
+	return bbDBType<T>()+"[]";
+}
+
+template<class T,int N> bbString bbDBValue( bbArray<T,N> **p ){
+	return "array?????";
 }
 
 #endif

+ 33 - 43
modules/monkey/native/bbdebug.cpp

@@ -10,6 +10,8 @@
 
 namespace bbDB{
 
+	int nextSeq;
+	
 	bbDBContext *currentContext;
 	
 #if _WIN32
@@ -40,25 +42,35 @@ namespace bbDB{
 #endif
 	}
 	
+	void emitVar( bbDBVar *v ){
+		bbString id=v->name;
+		bbString type=v->type->type();
+		bbString value=v->type->value( v->var );
+		bbString t=id+":"+type+"="+value+"\n";
+		printf( t.c_str() );
+	}
+	
 	void emitStack(){
 		bbDBVar *ev=currentContext->locals;
 		
 		for( bbDBFrame *f=currentContext->frames;f;f=f->succ ){
 
-			printf( ">%s;%s;%i\n",f->decl,f->srcFile,f->srcPos );
+			printf( ">%s;%s;%i;%i\n",f->decl,f->srcFile,f->srcPos,f->seq );
 			
 			for( bbDBVar *v=f->locals;v!=ev;++v ){
-				char id[64],type[64],value[128];
-				strcpy( id,v->name );
-				strcpy( type,v->type->name().c_str() );
-				strcpy( value,v->type->value( v->var ).c_str() );
-				printf( "%s:%s=%s\n",id,type,value );
+				emitVar( v );
 			}
 
 			ev=f->locals;
 		}
 	}
 	
+	void emitObject( bbObject *p ){
+		if( p ) p->dbEmit();
+		printf( "\n" );
+		fflush( stdout );
+	}
+	
 	void stop(){
 
 		currentContext->stopped=0;
@@ -73,19 +85,23 @@ namespace bbDB{
 		printf( "\n" );
 		fflush( stdout );
 		
-		char buf[256];
-		char *e=fgets( buf,256,stdin );
-		if( !e ) exit( -1 );
+		for(;;){
 		
-		switch( e[0] ){
-		case 's':currentContext->stopped=0;return;
-		case 'e':currentContext->stopped=1;return;
-		case 'l':currentContext->stopped=-1;return;
-		case 'r':currentContext->stopped=-0x10000000;return;
-		case 'q':exit( 0 );
+			char buf[256];
+			char *e=fgets( buf,256,stdin );
+			if( !e ) exit( -1 );
+			
+			switch( e[0] ){
+			case 's':currentContext->stopped=0;return;
+			case 'e':currentContext->stopped=1;return;
+			case 'l':currentContext->stopped=-1;return;
+			case 'r':currentContext->stopped=-0x10000000;return;
+			case '@':emitObject( (bbObject*)strtol( e+1,0,16 ) );continue;
+			case 'q':exit( 0 );return;
+			}
+			printf( "Unrecognized debug cmd: %s\n",buf );fflush( stdout );
+			exit( -1 );
 		}
-		printf( "Unrecognized debug cmd: %s\n",buf );fflush( stdout );
-		exit( -1 );
 	}
 	
 	bbArray<bbString> *stack(){
@@ -115,29 +131,3 @@ void bbDBContext::init(){
 bbDBContext::~bbDBContext(){
 	delete[] localsBuf;
 }
-
-template<> bbDBType *bbDBTypeOf( void* ){
-	struct type : public bbDBType{
-		bbString name(){ return "Void"; }
-	};
-	static type _type;
-	return &_type;
-}
-
-template<> bbDBType *bbDBTypeOf( bbInt* ){
-	struct type : public bbDBType{
-		bbString name(){ return "Int"; }
-		bbString value( void *var ){ return *(bbInt*)var; }
-	};
-	static type _type;
-	return &_type;
-}
-
-template<> bbDBType *bbDBTypeOf( bbString* ){
-	struct type : public bbDBType{
-		bbString name(){ return "String"; }
-		bbString value( void *var ){ return BB_T("\"")+*(bbString*)var+BB_T( "\"" ); }
-	};
-	static type _type;
-	return &_type;
-}

+ 73 - 29
modules/monkey/native/bbdebug.h

@@ -5,38 +5,74 @@
 #include "bbstring.h"
 
 struct bbDBFiber;
-
 struct bbDBFrame;
-
+struct bbDBVarType;
 struct bbDBVar;
 
-//subclasses can't add data!
-struct bbDBType{
-	virtual bbString name(){ return "<?>"; }
-	virtual bbString value( void *var ){ return "?????"; }
-//	virtual void members( bbDBVar *var,bbDBVar **vars ){}
-};
+inline bbString bbDBType( void *p ){
+	return "Void";
+}
+
+inline bbString bbDBValue( void *p ){
+	return "?????";
+}
+
+inline bbString bbDBType( bbInt *p ){
+	return "Int";
+}
+
+inline bbString bbDBValue( bbInt *p ){
+	return bbString( *p );
+}
 
-template<class T> bbDBType *bbDBTypeOf( T* ){
-	static bbDBType _type;
-	return &_type;
+inline bbString bbDBType( bbFloat *p ){
+	return "Float";
 }
 
-template<class T> bbDBType *bbDBTypeOf( T** ){
-	struct type : public bbDBType{
-		bbString name(){ return bbDBTypeOf( (T*)0 )->name()+" Ptr"; }
-	};
-	static type _type;
-	return &_type;
+inline bbString bbDBValue( bbFloat *p ){
+	return bbString( *p );
 }
 
-template<class T> bbDBType *bbDBTypeOf(){
-	return bbDBTypeOf( (T*)0 );
+inline bbString bbDBType( bbString *p ){
+	return "String";
 }
 
+inline bbString bbDBValue( bbString *p ){
+	return BB_T("\"")+(*p)+"\"";
+}
+
+template<class T> bbString bbDBType( T **p ){
+	return bbDBType( (void*)0 )+" Ptr";
+}
+
+template<class T> bbString bbDBType(){
+	return bbDBType( (T*)0 );
+}
+
+struct bbDBVarType{
+	virtual bbString type()=0;
+	virtual bbString value( void *p )=0;
+};
+
+template<class T> struct bbDBVarType_t : public bbDBVarType{
+
+	bbString type(){
+		return bbDBType( (T*)0 );
+	}
+	
+	bbString value( void *p ){
+		return bbDBValue( (T*)p );
+	}
+	
+	static bbDBVarType_t info;
+};
+
+template<class T> bbDBVarType_t<T> bbDBVarType_t<T>::info;
+
 struct bbDBVar{
+
 	const char *name;
-	bbDBType *type;
+	bbDBVarType *type;
 	void *var;
 };
 
@@ -46,7 +82,7 @@ struct bbDBContext{
 	bbDBVar *localsBuf=nullptr;
 	bbDBVar *locals=nullptr;
 	int stopped;
-	
+
 	~bbDBContext();
 	
 	void init();
@@ -54,6 +90,8 @@ struct bbDBContext{
 
 namespace bbDB{
 
+	extern int nextSeq;
+
 	extern bbDBContext *currentContext;
 	
 	void init();
@@ -71,13 +109,16 @@ struct bbDBFrame{
 	const char *decl;
 	const char *srcFile;
 	int srcPos;
+	int seq;
 	
 	bbDBFrame( const char *decl,const char *srcFile ):succ( bbDB::currentContext->frames ),locals( bbDB::currentContext->locals ),decl( decl ),srcFile( srcFile ){
 		bbDB::currentContext->frames=this;
 		--bbDB::currentContext->stopped;
+		seq=++bbDB::nextSeq;
 	}
 	
 	~bbDBFrame(){
+		++bbDB::nextSeq;
 		++bbDB::currentContext->stopped;
 		bbDB::currentContext->locals=locals;
 		bbDB::currentContext->frames=succ;
@@ -100,17 +141,20 @@ inline void bbDBStmt( int srcPos ){
 	if( bbDB::currentContext->stopped>=0 ) bbDB::stopped();
 }
 
-template<class T> void bbDBLocal( const char *name,T *var ){
+template<class T> void bbDBEmit( const char *name,T *var ){
+	bbDBVarType *type=&bbDBVarType_t<T>::info;
+	puts( (BB_T( name )+":"+type->type()+"="+type->value( var )).c_str() );
+}
+
+template<class T> void bbDBEmit( const char *name,bbGCVar<T> *p ){
+	T *var=p->get();return bbDBEmit( name,&var );
+}
+
+template<class T> void bbDBLocal ( const char *name,T *var ){
 	bbDB::currentContext->locals->name=name;
-	bbDB::currentContext->locals->type=bbDBTypeOf<T>();
+	bbDB::currentContext->locals->type=&bbDBVarType_t<T>::info;
 	bbDB::currentContext->locals->var=var;
 	++bbDB::currentContext->locals;
 }
 
-template<> bbDBType *bbDBTypeOf( void* );
-
-template<> bbDBType *bbDBTypeOf( bbInt* );
-
-template<> bbDBType *bbDBTypeOf( bbString* );
-
 #endif

+ 5 - 6
modules/monkey/native/bbfunction.h

@@ -258,13 +258,12 @@ template<class R,class...A> int bbCompare( const bbFunction<R(A...)> &x,const bb
 	return x._rep->compare( y._rep );
 }
 
-template<class R,class...A> struct bbDBFuncType : public bbDBType{
-	bbString name(){ return bbDBTypeOf<R>()->name()+"(...)"; }
-};
+template<class R,class...A> bbString bbDBType( bbFunction<R(A...)> *p ){
+	return bbDBType<R>()+"()";
+}
 
-template<class R,class...A> bbDBType *bbDBTypeOf( bbFunction<R(A...)>* ){
-	static bbDBFuncType<R,A...> _type;
-	return &_type;
+template<class R,class...A> bbString bbDBValue( bbFunction<R(A...)> *p ){
+	return "function?????";
 }
 
 #endif

+ 39 - 3
modules/monkey/native/bbgc.h

@@ -173,17 +173,17 @@ namespace bbGC{
 }
 
 template<class T> struct bbGCVar{
+
+	public:
+	
 	T *_ptr;
 	
 	void enqueue(){
-	
 #if BBGC_INCREMENTAL
 		bbGC::enqueue( dynamic_cast<bbGCNode*>( _ptr ) );
 #endif
 	}
 	
-	public:
-	
 	bbGCVar():_ptr( nullptr ){
 	}
 	
@@ -220,6 +220,42 @@ template<class T> struct bbGCVar{
 	}
 };
 
+template<class T> struct bbGCRootVar : public bbGCVar<T>,public bbGCRoot{
+	
+	using bbGCVar<T>::_ptr;
+	using bbGCVar<T>::enqueue;
+
+	bbGCRootVar(){
+	}
+	
+	bbGCRootVar( T *p ){
+		_ptr=p;
+		enqueue();
+	}
+	
+	bbGCRootVar( const bbGCVar<T> &p ){
+		_ptr=p._ptr;
+		enqueue();
+	}
+	
+
+	bbGCRootVar &operator=( T *p ){
+		_ptr=p;
+		enqueue();
+		return *this;
+	}
+	
+	bbGCRootVar &operator=( const bbGCVar<T> &p ){
+		_ptr=p._ptr;
+		enqueue();
+		return *this;
+	}
+	
+	virtual void gcMark(){
+		bbGC::enqueue( dynamic_cast<bbGCNode*>( _ptr ) );
+	}
+};
+
 inline void bbGCMark( bbBool ){}
 inline void bbGCMark( bbByte ){}
 inline void bbGCMark( bbUByte ){}

+ 0 - 9
modules/monkey/native/bbobject.cpp

@@ -14,12 +14,3 @@ bbException::bbException( bbString message ):bbException(){
 
 	_message=message;
 }
-
-template<> bbDBType *bbDBTypeOf( bbObject** ){
-	struct type : public bbDBType{
-		bbString name(){ return "Object"; }
-	};
-	static type _type;
-	return &_type;
-}
-

+ 22 - 8
modules/monkey/native/bbobject.h

@@ -19,7 +19,7 @@ struct bbObject : public bbGCNode{
 		return "monkey.Object";
 	}
 	
-	virtual void debugVars( bbDBVar *p )const{
+	virtual void dbEmit(){
 	}
 
 	void *operator new( size_t size ){
@@ -74,14 +74,28 @@ template<class T,class...A> T *bbGCNew( A...a ){
 	return p;
 }
 
-inline bbDBType *bbDBTypeOf( const bbObject*& ){
-	struct type : public bbDBType{
-		bbString name(){ return "Object"; }
-	};
-	static type _type;
-	return &_type;
+inline bbString bbDBObjectValue( bbObject *p ){
+	char buf[64];
+	sprintf( buf,"@%p",p );
+	return buf;
 }
 
-template<> bbDBType *bbDBTypeOf( bbObject** );
+inline bbString bbDBInterfaceValue( bbInterface *p ){
+	return bbDBObjectValue( dynamic_cast<bbObject*>( p ) );
+}
+
+inline bbString bbDBStructValue( void *p ){
+	char buf[64];
+	sprintf( buf,"&%p",p );
+	return buf;
+}
+
+inline bbString bbDBType( bbObject **p ){
+	return "Object";
+}
+
+inline bbString bbDBValue( bbObject **p ){
+	return bbDBObjectValue( *p );
+}
 
 #endif

+ 1 - 0
modules/monkey/native/bbtypes.h

@@ -21,6 +21,7 @@ typedef unsigned short bbChar;
 class bbString;
 template<class T> class bbFunction;
 template<class T,int D=1> class bbArray;
+template<class T> struct bbGCVar;
 
 bbString bbTypeName( const char *type );