From bdaf02e77b5fa7c19abe691380265984ca9ce40c Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Thu, 3 Mar 2016 23:07:11 +0000
Subject: [PATCH]

---
 include/zipr_dollop_man.h | 18 ++++++++++------
 src/memory_space.cpp      | 16 +++++++++------
 src/zipr.cpp              | 38 +++++++++++++---------------------
 src/zipr_dollop_man.cpp   | 43 +++++++++++++++++++++++++++++++--------
 4 files changed, 70 insertions(+), 45 deletions(-)

diff --git a/include/zipr_dollop_man.h b/include/zipr_dollop_man.h
index 3e3e71c..e532497 100644
--- a/include/zipr_dollop_man.h
+++ b/include/zipr_dollop_man.h
@@ -33,6 +33,12 @@
 
 #include <dollop.h>
 
+
+typedef		std::set<Dollop_t*> DollopList_t;
+typedef		std::map<libIRDB::Instruction_t*,Dollop_t*>  InsnToDollopMap_t;
+typedef		std::list<DollopPatch_t*>  DollopPatchList_t;
+typedef		std::map<Dollop_t*, DollopPatchList_t > DollopToDollopPatchListMap_t;
+
 class ZiprDollopManager_t : public DollopManager_t {
 	public:
 		ZiprDollopManager_t() : m_refresh_stats(true) {}
@@ -76,10 +82,10 @@ class ZiprDollopManager_t : public DollopManager_t {
 		/*
 		 * Iteration functions.
 		 */
-		std::list<Dollop_t*>::const_iterator dollops_begin() {
+		DollopList_t::iterator dollops_begin() {
 			return m_dollops.begin();
 		}
-		std::list<Dollop_t*>::const_iterator dollops_end() {
+		DollopList_t::iterator dollops_end() {
 			return m_dollops.end();
 		}
 
@@ -102,10 +108,10 @@ class ZiprDollopManager_t : public DollopManager_t {
 		/*
 		 * Support variables.
 		 */
-		std::list<Dollop_t*> m_dollops;
-		std::map<libIRDB::Instruction_t*,Dollop_t*> m_insn_to_dollop;
-		std::list<DollopPatch_t*> m_patches;
-		std::map<Dollop_t*, std::list<DollopPatch_t*>> m_patches_to_dollops;
+		DollopList_t m_dollops;
+		InsnToDollopMap_t m_insn_to_dollop;
+		DollopPatchList_t m_patches;
+		DollopToDollopPatchListMap_t m_patches_to_dollops;
 
 		/*
 		 * Statistics.
diff --git a/src/memory_space.cpp b/src/memory_space.cpp
index 1d8f831..421c8a9 100644
--- a/src/memory_space.cpp
+++ b/src/memory_space.cpp
@@ -107,9 +107,9 @@ void ZiprMemorySpace_t::MergeFreeRange(RangeAddress_t addr)
 		Range_t nnr(addr, r.GetEnd());
 		if (m_verbose)
 		{
-			printf("Expanded range:\n");
-			printf("from: %p to %p\n", (void*)r.GetStart(), (void*)r.GetEnd());
-			printf("to: %p to %p\n", (void*)nnr.GetStart(), (void*)nnr.GetEnd());
+			printf("Expanded range: ");
+			printf("from: (%p - %p) ", (void*)r.GetStart(), (void*)r.GetEnd());
+			printf("to: (%p - %p) \n", (void*)nnr.GetStart(), (void*)nnr.GetEnd());
 		}
 		nr = nnr;
 		if(itm1!=free_ranges.end())
@@ -132,9 +132,9 @@ void ZiprMemorySpace_t::MergeFreeRange(RangeAddress_t addr)
 		Range_t nnr(r.GetStart(), addr);
 		if (m_verbose)
 		{
-			printf("Expanded range:\n");
-			printf("from: %p to %p\n", (void*)r.GetStart(), (void*)r.GetEnd());
-			printf("to: %p to %p\n", (void*)nnr.GetStart(), (void*)nnr.GetEnd());
+			printf("Expanded range: ");
+			printf("from: (%p - %p) ", (void*)r.GetStart(), (void*)r.GetEnd());
+			printf("to: (%p - %p) \n", (void*)nnr.GetStart(), (void*)nnr.GetEnd());
 		}
 		nr = nnr;
 		free_ranges.erase(itm1);
@@ -230,6 +230,10 @@ Range_t ZiprMemorySpace_t::GetFreeRange(int size)
 			big_range=r;;
 		if(r.GetEnd() - r.GetStart() >= (unsigned) size)
 			v.push_back(r);
+
+		// that's enough randomization
+		if(v.size() > 100)
+			break;
 	}
 	if(v.size()==0)
 		return big_range;	
diff --git a/src/zipr.cpp b/src/zipr.cpp
index ca315ec..a8a2956 100644
--- a/src/zipr.cpp
+++ b/src/zipr.cpp
@@ -711,8 +711,9 @@ void ZiprImpl_t::PreReserve2ByteJumpTargets()
 		 */
 		for(int size=5;size>0;size-=3) 
 		{
-			if (m_verbose)
-				printf("Looking for %d-byte jump targets to pre-reserve.\n", size);
+
+			//if (m_verbose)
+			//	printf("Looking for %d-byte jump targets to pre-reserve.\n", size);
 			for(int i=120;i>=-120;i--)
 			{
 				if(memory_space.AreBytesFree(addr+i,size))
@@ -977,8 +978,7 @@ void ZiprImpl_t::ReservePinnedInstructions()
 		}
 
 		if (m_verbose) {
-			printf("Working two byte pinning decision at %p for:\n", 
-					(void*)addr);
+			printf("Working two byte pinning decision at %p for: ", (void*)addr);
 			printf("%s\n", upinsn->GetComment().c_str());
 		}
 
@@ -986,6 +986,7 @@ void ZiprImpl_t::ReservePinnedInstructions()
 		// if the byte at x+1 is free, we can try a 2-byte jump (which may end up being converted to a 5-byte jump later).
 		if (FindPinnedInsnAtAddr(addr+1)==NULL)
 		{
+			/* so common it's not worth printing 
 			if (m_verbose)
 			{
 				printf("Can fit two-byte pin (%p-%p).  fid=%d\n", 
@@ -993,6 +994,7 @@ void ZiprImpl_t::ReservePinnedInstructions()
 					(void*)(addr+sizeof(bytes)-1),
 					upinsn->GetAddress()->GetFileID());
 			}
+			*/
 		
 			/*
 			 * Assert that the space is free.  We already checked that it should be 
@@ -1302,7 +1304,7 @@ void ZiprImpl_t::Fix2BytePinnedInstructions()
 
 void ZiprImpl_t::WriteDollops()
 {
-	list<Dollop_t*>::const_iterator it, it_end;
+	DollopList_t::iterator it, it_end;
 	for (it = m_dollop_mgr.dollops_begin(),
 	     it_end = m_dollop_mgr.dollops_end();
 	     it != it_end;
@@ -1329,22 +1331,6 @@ void ZiprImpl_t::WriteDollops()
 	}
 }
 
-#if 0
-class placement_queue_comp
-{
-	public:
-	bool operator ()(const pair<Dollop_t*,RangeAddress_t> &a,
-	                 const pair<Dollop_t*,RangeAddress_t> &b) 
-	{
-		if((a.first)->front()->Instruction()->GetAddress()->GetVirtualOffset()==0||
-		   (b.first)->front()->Instruction()->GetAddress()->GetVirtualOffset()==0)
-			return (a.second<b.second);
-		return (a.first)->front()->Instruction()->GetAddress()->GetVirtualOffset()<
-		       (b.first)->front()->Instruction()->GetAddress()->GetVirtualOffset();
-	}
-};
-#endif
-
 void ZiprImpl_t::PlaceDollops()
 {
 	int count_pins=0;
@@ -1370,7 +1356,6 @@ void ZiprImpl_t::PlaceDollops()
 		target_dollop = m_dollop_mgr.GetContainingDollop(target_insn);
 		assert(target_dollop);
 
-		//placement_queue.push_back(pair<Dollop_t*,RangeAddress_t>(target_dollop,patch.GetAddress()));
 		placement_queue.insert(pair<Dollop_t*,RangeAddress_t>(target_dollop,patch.GetAddress()));
 		if (m_verbose) {
 			cout << "Original: " << std::hex << target_insn->
@@ -1738,16 +1723,21 @@ void ZiprImpl_t::CreateDollops()
 {
 	multimap<const UnresolvedUnpinned_t,Patch_t>::const_iterator pin_it,
 	                                                             pin_it_end;
+	int dollop_count=0;
+	cout<<"Attempting to create "<<patch_list.size()<<" dollops for the pins.";
 	for (pin_it = patch_list.begin(), pin_it_end = patch_list.end();
 	     pin_it != pin_it_end;
 	     pin_it++)
 	{
 		UnresolvedUnpinned_t uu = (*pin_it).first;
 		m_dollop_mgr.AddNewDollops(uu.GetInstruction());
+
+		if((dollop_count++ % 100) == 0)
+			cout<<"."<<flush;
 	}
+	cout<<"Done!  Updating all Targets"<<endl;
 	m_dollop_mgr.UpdateAllTargets();
-	if (m_verbose)
-		cout << "Created " <<std::dec<< m_dollop_mgr.Size() << " dollops." << endl;
+	cout << "Created " <<std::dec<< m_dollop_mgr.Size() << " total dollops." << endl;
 }
 
 void ZiprImpl_t::OptimizePinnedInstructions()
diff --git a/src/zipr_dollop_man.cpp b/src/zipr_dollop_man.cpp
index 665c7e8..9752f06 100644
--- a/src/zipr_dollop_man.cpp
+++ b/src/zipr_dollop_man.cpp
@@ -145,12 +145,20 @@ namespace zipr {
 	}
 
 	Dollop_t *ZiprDollopManager_t::GetContainingDollop(libIRDB::Instruction_t *insn) {
+#if 0
 		try {
 			return m_insn_to_dollop.at(insn);
 		} catch (const std::out_of_range &oor) {
 			return NULL;
 		}
 		return NULL;
+#else
+		InsnToDollopMap_t::iterator it=m_insn_to_dollop.find(insn);
+		if(it!=m_insn_to_dollop.end())
+			return it->second;
+		return NULL;
+			
+#endif
 	}
 
 	void ZiprDollopManager_t::AddDollops(Dollop_t *dollop_head) {
@@ -179,7 +187,7 @@ namespace zipr {
 		/*
 		 * Populate/update the instruction-to-dollop map.
 		 */
-		list<DollopEntry_t*>::const_iterator it, it_end;
+		std::list<DollopEntry_t*>::iterator it, it_end;
 		for (it = dollop->begin(), it_end = dollop->end();
 		     it != it_end;
 				 it++) {
@@ -189,8 +197,14 @@ namespace zipr {
 		 * Push the actual dollop onto the list of dollops
 		 * if it's not already there.
 		 */
+#if 0		
 		if (m_dollops.end()==std::find(m_dollops.begin(), m_dollops.end(), dollop))
 			m_dollops.push_back(dollop);
+#else
+			m_dollops.insert(dollop);
+
+#endif
+	
 		m_refresh_stats = true;
 	}
 
@@ -198,8 +212,11 @@ namespace zipr {
 		list<DollopEntry_t*>::iterator it, it_end;
 		bool changed = false;
 		bool local_changed = false;
+		int local_changed_count=0;
+		int and_count=0;
 		do {
 			local_changed = false;
+			local_changed_count++;
 			for (it = dollop->begin(), it_end = dollop->end();
 			     it != it_end;
 					 /* nop */) {
@@ -208,6 +225,7 @@ namespace zipr {
 				if (entry->Instruction() &&
 				    entry->Instruction()->GetTarget()) {
 					Dollop_t *new_target=AddNewDollops(entry->Instruction()->GetTarget());
+					and_count++;
 
 					/*
 					 * In the case there is a change, we have to restart.
@@ -223,27 +241,34 @@ namespace zipr {
 					}
 				}
 			}
-		} while (local_changed);
+			
+		} while (false); // while (local_changed);
 		return changed;
 	}
 
 	void ZiprDollopManager_t::UpdateAllTargets(void) {
-		std::list<Dollop_t *>::const_iterator it, it_end;
+		DollopList_t::iterator it, it_end;
 		bool changed = false;
+		int changed_count=0;
+		int update_count=0;
 		do {
 			changed = false;
-			for (it = m_dollops.begin(), it_end = m_dollops.end();
-		     it != m_dollops.end();
-				 /* nop */) {
+			for (it = m_dollops.begin(), it_end = m_dollops.end(); it != m_dollops.end(); /* nop */) 
+			{
 				Dollop_t *entry = *it;
 				it++;
 				changed |= UpdateTargets(entry);
+				update_count++;
+				if((update_count%1000000) == 0 )
+					cout<<"number of dollops="<<dec<<m_dollops.size()<<".  "<<dec<<update_count<<" iterations attempted."<<endl;
 			}
+			changed_count++;
 		} while (changed);
+		cout<<"All Targets updated.  changed_count="<<dec<<changed_count<<". Update_count="<<update_count<<"."<<endl;
 	}
 
 	std::ostream &operator<<(std::ostream &out, const ZiprDollopManager_t &dollop_man) {
-		std::list<Dollop_t *>::const_iterator it, it_end;
+		DollopList_t::iterator it, it_end;
 
 		for (it = dollop_man.m_dollops.begin(), it_end = dollop_man.m_dollops.end();
 		     it != it_end;
@@ -261,7 +286,7 @@ namespace zipr {
 		m_total_dollop_entries = 0;
 		m_total_dollops = Size();
 
-		std::list<Dollop_t*>::iterator dollop_it, dollop_it_end;
+		DollopList_t::iterator dollop_it, dollop_it_end;
 		for (dollop_it = m_dollops.begin(), dollop_it_end = m_dollops.end();
 		     dollop_it != m_dollops.end();
 				 dollop_it++)
@@ -315,7 +340,7 @@ namespace zipr {
 			 * Now loop through the dollops and
 			 * record those contained in this range.
 			 */
-			list<Dollop_t*>::const_iterator dollop_it, dollop_it_end;
+			DollopList_t::iterator dollop_it, dollop_it_end;
 			Range_t current_range = *range_it;
 			map<RangeAddress_t, Dollop_t*> dollops_in_range;
 			map<RangeAddress_t, Dollop_t*>::const_iterator dollops_in_range_it,
-- 
GitLab