Ver código fonte

sort blocks per depth, more accurate locate results

Nicolas Cannasse 8 anos atrás
pai
commit
a4993f1a9f
2 arquivos alterados com 86 adições e 24 exclusões
  1. 30 13
      other/memory/Block.hx
  2. 56 11
      other/memory/Memory.hx

+ 30 - 13
other/memory/Block.hx

@@ -81,6 +81,8 @@ class Block {
 	public var type(default, set) : TType;
 	public var typeKind : BlockTypeKind;
 
+	public var depth : Int = -1;
+
 	public var subs : Array<Block>; // can be null
 	public var parents : Array<Block>; // if multiple owners
 
@@ -111,27 +113,42 @@ class Block {
 		b.subs.push(this);
 	}
 
+	public function getParents() {
+		return parents != null ? parents : owner == null ? [] : [owner];
+	}
+
+	public function markDepth() {
+		var d = depth + 1;
+		var all = subs;
+		while( all.length > 0 ) {
+			var out = [];
+			for( b in all ) {
+				if( b.depth < 0 || b.depth > d ) {
+					b.depth = d;
+					if( b.subs != null ) for( s in b.subs ) out.push(s);
+				}
+			}
+			all = out;
+			d++;
+		}
+	}
+
 	public function finalize() {
 		if( parents == null ) return;
 
 		inline function getPriority(b:Block) {
-			if( b.type == null ) return -2;
-			if( !b.type.hasPtr ) return -1; // false positive
+			var d = -b.depth * 5;
+			if( b.type == null ) return d-2;
+			if( !b.type.hasPtr ) return d-1; // false positive
 			return switch( b.type.t ) {
-			case HFun(_): 0;
-			case HVirtual(_): 1;
-			default: 2;
+			case HFun(_): d;
+			case HVirtual(_): d+1;
+			default: d+2;
 			}
 		}
 
-		var pbest = getPriority(owner);
-		for( p in parents ) {
-			var prio = getPriority(p);
-			if( prio > pbest ) {
-				owner = p;
-				prio = pbest;
-			}
-		}
+		parents.sort(function(p1, p2) return getPriority(p2) - getPriority(p1));
+		owner = parents[0];
 	}
 
 	function removeParent( p : Block ) {

+ 56 - 11
other/memory/Memory.hx

@@ -309,6 +309,7 @@ class Memory {
 		var broot = new Block();
 		broot.type = new TType(types.length, HAbstract("roots"));
 		types.push(broot.type);
+		broot.depth = 0;
 		broot.addParent(all);
 		var rid = 0;
 		for( g in code.globals ) {
@@ -338,11 +339,14 @@ class Memory {
 
 		// look in stacks (low priority of ownership)
 		var tstacks = new TType(types.length, HAbstract("stack"));
+		var bstacks = [];
 		types.push(tstacks);
 		for( s in stacks ) {
 			var bstack = new Block();
+			bstack.depth = 10000;
 			bstack.type = tstacks;
 			bstack.addParent(all);
+			bstacks.push(bstack);
 			for( r in s.contents ) {
 				var b = pointerBlock.get(r);
 				if( b != null )
@@ -357,6 +361,35 @@ class Memory {
 				f.b.type.falsePositiveIndexes[f.idx]++;
 			}
 
+		// assign depths
+
+		Sys.println("Computing depths...");
+		broot.markDepth();
+		for( b in bstacks ) b.markDepth();
+
+		var changed = -1;
+		while( changed != 0 ) {
+			changed = 0;
+			for( b in blocks ) {
+				var minD = -1;
+				if( b.parents == null ) {
+					if( b.owner != null && b.owner.depth >= 0 )
+						minD = b.owner.depth;
+				} else {
+					for( p in b.parents )
+						if( p.depth >= 0 && (minD < 0 || p.depth < minD) )
+							minD = p.depth;
+				}
+				if( minD >= 0 ) {
+					minD++;
+					if( b.depth < 0 || b.depth > minD ) {
+						b.depth = minD;
+						changed++;
+					}
+				}
+			}
+		}
+
 		for( b in blocks )
 			b.finalize();
 
@@ -531,19 +564,9 @@ class Memory {
 					var ol = [owner];
 					tl.push(owner.type == null ? 0 : owner.type.tid);
 					var k : Int = up;
-					while( owner.owner != null && k-- > 0 ) {
+					while( owner.owner != null && k-- > 0 && ol.indexOf(owner.owner) < 0 && owner.owner != all ) {
 						var prev = owner;
 						owner = owner.owner;
-						if( ol.indexOf(owner) >= 0 && prev.parents != null ) {
-							var found = false;
-							for( p in prev.parents )
-								if( ol.indexOf(p) < 0 ) {
-									owner = p;
-									found = true;
-									break;
-								}
-							if( !found ) break;
-						}
 						ol.push(owner);
 						tl.unshift(owner.type == null ? 0 : owner.type.tid);
 					}
@@ -553,6 +576,26 @@ class Memory {
 		ctx.print();
 	}
 
+	function parents( tstr : String, up = 0 ) {
+		var lt = null;
+		for( t in types )
+			if( t.t.toString() == tstr ) {
+				lt = t;
+				break;
+			}
+		if( lt == null ) {
+			log("Type not found");
+			return;
+		}
+
+		var ctx = new Stats(this);
+		for( b in blocks )
+			if( b.type == lt )
+				for( b in b.getParents() )
+					ctx.addPath([if( b.type == null ) 0 else b.type.tid], 0);
+		ctx.print();
+	}
+
 	function subs( tstr : String, down = 0 ) {
 		var lt = null;
 		for( t in types )
@@ -646,6 +689,8 @@ class Memory {
 				m.printUnknown();
 			case "locate":
 				m.locate(args.shift(), Std.parseInt(args.shift()));
+			case "parents":
+				m.parents(args.shift());
 			case "subs":
 				m.subs(args.shift(), Std.parseInt(args.shift()));
 			case "part":