From fc9337aa9c0f9ab0bb0f4067140e814d40b24080 Mon Sep 17 00:00:00 2001
From: whh8b <whh8b@git.zephyr-software.com>
Date: Fri, 14 Apr 2017 02:50:08 +0000
Subject: [PATCH] Update to latest SDK and implementation for callbacks.

---
 include/plugin_man.h |  2 +-
 src/plugin_man.cpp   |  4 ++--
 src/zipr.cpp         | 24 +++++++++++++++++-------
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/include/plugin_man.h b/include/plugin_man.h
index f32de9a..23d38de 100644
--- a/include/plugin_man.h
+++ b/include/plugin_man.h
@@ -45,7 +45,7 @@ class ZiprPluginManager_t : public ZiprPluginInterface_t
 
 		virtual bool DoPluginsPlop(libIRDB::Instruction_t*,std::list<DLFunctionHandle_t>&);
 
-		virtual bool DoesPluginAddress(const Dollop_t *, const RangeAddress_t &, Range_t &, bool &coalesce, DLFunctionHandle_t &);
+		virtual bool DoesPluginAddress(const Dollop_t *, const RangeAddress_t &, Range_t &, bool &coalesce, bool &fallthrough_allowed, DLFunctionHandle_t &);
 		virtual bool DoesPluginRetargetCallback(const RangeAddress_t &, const DollopEntry_t *, RangeAddress_t &, DLFunctionHandle_t &) ;
 		virtual bool DoesPluginRetargetPin(const RangeAddress_t &, const Dollop_t *, RangeAddress_t &, DLFunctionHandle_t &) ;
 
diff --git a/src/plugin_man.cpp b/src/plugin_man.cpp
index 7d67996..43fcdb9 100644
--- a/src/plugin_man.cpp
+++ b/src/plugin_man.cpp
@@ -117,13 +117,13 @@ RangeAddress_t ZiprPluginManager_t::PlaceScoopsEnd(const RangeAddress_t max_addr
 }
 
 
-bool ZiprPluginManager_t::DoesPluginAddress(const Dollop_t *dollop, const RangeAddress_t &source, Range_t &place, bool &coalesce, DLFunctionHandle_t &placer)
+bool ZiprPluginManager_t::DoesPluginAddress(const Dollop_t *dollop, const RangeAddress_t &source, Range_t &place, bool &coalesce, bool &fallthrough_allowed, DLFunctionHandle_t &placer)
 {
 	DLFunctionHandleSet_t::iterator it=m_handleList.begin();
 	for(m_handleList.begin();it!=m_handleList.end();++it)
 	{
 		ZiprPluginInterface_t* zpi=(ZiprPluginInterface_t*)*it;
-		if (Must == zpi->AddressDollop(dollop, source, place, coalesce))
+		if (Must == zpi->AddressDollop(dollop, source, place, coalesce, fallthrough_allowed))
 		{
 			placer = zpi;
 			return true;
diff --git a/src/zipr.cpp b/src/zipr.cpp
index d531f1f..df8eec4 100644
--- a/src/zipr.cpp
+++ b/src/zipr.cpp
@@ -2069,7 +2069,7 @@ void ZiprImpl_t::PlaceDollops()
 		Dollop_t *fallthrough = NULL;
 		bool continue_placing = false;
 		bool am_coalescing = false;
-		bool allowed_coalescing = true;
+		bool allowed_coalescing = true, allowed_fallthrough = true;
 		bool initial_placement_abuts_pin = false;
 		bool initial_placement_abuts_fallthrough = false;
 		bool fits_entirely = false;
@@ -2106,6 +2106,7 @@ void ZiprImpl_t::PlaceDollops()
 		                              from_address,
 		                              placement,
 		                              allowed_coalescing,
+		                              allowed_fallthrough,
 		                              placer))
 		{
 			placed = true;
@@ -2161,7 +2162,7 @@ void ZiprImpl_t::PlaceDollops()
 			 * that we can keep from placing a jump to the dollo
 			 * and instead just fallthrough.
 			 */
-			if (to_place->FallthroughDollop()) 
+			if (to_place->FallthroughDollop() && allowed_fallthrough) 
 			{
 				/*
 				 * Find out where the fallthrough dollop is placed.
@@ -2182,6 +2183,7 @@ void ZiprImpl_t::PlaceDollops()
 				{
 					Range_t fallthrough_placement;
 					bool fallthrough_allowed_coalescing = false;
+					bool fallthrough_allowed_fallthrough = false;
 					DLFunctionHandle_t fallthrough_placer = NULL;
 					/*
 					 * Prospectively get the place for this dollop. That way 
@@ -2191,6 +2193,7 @@ void ZiprImpl_t::PlaceDollops()
 		                                    from_address,
 		                                    fallthrough_placement,
 		                                    fallthrough_allowed_coalescing,
+		                                    fallthrough_allowed_fallthrough,
 		                                    fallthrough_placer))
 					{
 						fallthrough_dollop_place = fallthrough_placement.GetStart();
@@ -2247,6 +2250,7 @@ void ZiprImpl_t::PlaceDollops()
 		}
 
 		cur_addr = placement.GetStart();
+		cout << "Adjusting cur_addr to " << std::hex << cur_addr << " at A." << endl;
 		has_fallthrough = (to_place->FallthroughDollop() != NULL);
 
 		if (m_verbose)
@@ -2488,6 +2492,7 @@ void ZiprImpl_t::PlaceDollops()
 					dollop_entry->Place(cur_addr);
 					cur_addr+=DetermineWorstCaseDollopEntrySize(dollop_entry,
 					                                      false);
+					cout << "Adjusting cur_addr to " << std::hex << cur_addr << " at B." << endl;
 					if (dollop_entry->TargetDollop())
 					{
 						if (m_verbose)
@@ -2655,6 +2660,7 @@ void ZiprImpl_t::PlaceDollops()
 					patch_de->TargetDollop(fallthrough);
 					patch_de->Place(cur_addr);
 					cur_addr+=DetermineWorstCaseDollopEntrySize(patch_de, false);
+					cout << "Adjusting cur_addr to " << std::hex << cur_addr << " at C." << endl;
 
 					to_place->push_back(patch_de);
 					to_place->FallthroughPatched(true);
@@ -2665,7 +2671,8 @@ void ZiprImpl_t::PlaceDollops()
 								    " because fallthrough is placed" : "")
 						     << string((!allowed_coalescing) ?
 								    " because I am not allowed" : "")
-						     << "; Added jump (at " << std::hex << patch_de->Place() << ") "
+						     << "; Added jump (via " << std::hex << patch_de
+						     << " at " << std::hex << patch_de->Place() << ") "
 						     << "to fallthrough dollop (" << std::hex 
 						     << fallthrough << ")." << endl;
 
@@ -3234,12 +3241,12 @@ RangeAddress_t ZiprImpl_t::PlopDollopEntry(
 		insn->SetDataBits(raw_data);
 	}
 
-	if(entry->TargetDollop())
+	if(entry->TargetDollop() && entry->Instruction()->GetCallback()=="")
 	{
 		RangeAddress_t target_address = 0;
 		Instruction_t *target_insn = entry->TargetDollop()->front()->Instruction();
 
-		if (override_target != 0)
+		if (override_target == 0)
 		{	
 			if (final_insn_locations.end() != final_insn_locations.find(target_insn))
 				target_address = final_insn_locations[target_insn];
@@ -3247,7 +3254,8 @@ RangeAddress_t ZiprImpl_t::PlopDollopEntry(
 		else
 		{
 			if (m_verbose)
-				cout << "Plopping with overriden target: " 
+				cout << "Plopping with overriden target: Was: " 
+				     << std::hex << target_address << " Is: "
 				     << std::hex << override_target << endl;
 			target_address = override_target;
 		}
@@ -4098,7 +4106,7 @@ void ZiprImpl_t::UpdateCallbacks()
 		DollopEntry_t *entry=it->first;
 		Instruction_t *insn = entry->Instruction();
 		RangeAddress_t at=it->second;
-		RangeAddress_t to=FindCallbackAddress(end_of_new_space,start_addr,insn->GetCallback());
+		RangeAddress_t to=0x0;//FindCallbackAddress(end_of_new_space,start_addr,insn->GetCallback());
 		DLFunctionHandle_t patcher = NULL;
 
 		if (plugman.DoesPluginRetargetCallback(at, entry, to, patcher))
@@ -4117,7 +4125,9 @@ void ZiprImpl_t::UpdateCallbacks()
 			PatchCall(at,to);
 		}
 		else
+		{
 			CallToNop(at);
+		}
 	}
 }
 
-- 
GitLab