[Avida-SVN] r1383 - in development: source/cpu support/config

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sun Mar 4 19:02:23 PST 2007


Author: brysonda
Date: 2007-03-04 22:02:22 -0500 (Sun, 04 Mar 2007)
New Revision: 1383

Modified:
   development/source/cpu/cHardwareExperimental.cc
   development/source/cpu/cHardwareExperimental.h
   development/source/cpu/cHeadCPU.cc
   development/source/cpu/cHeadCPU.h
   development/support/config/instset-experimental.cfg
Log:
Add Goto instruction to cHardwareExperimental.   Also implement support for marking search target labels as executed.

Modified: development/source/cpu/cHardwareExperimental.cc
===================================================================
--- development/source/cpu/cHardwareExperimental.cc	2007-03-04 03:40:14 UTC (rev 1382)
+++ development/source/cpu/cHardwareExperimental.cc	2007-03-05 03:02:22 UTC (rev 1383)
@@ -76,12 +76,10 @@
      in the same order in tInstLibEntry<tMethod> s_f_array, and these entries must
      be the first elements of s_f_array.
      */
-    tInstLibEntry<tMethod>("nop-A", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation instruction; modifies other instructions"),
-    tInstLibEntry<tMethod>("nop-B", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation instruction; modifies other instructions"),
-    tInstLibEntry<tMethod>("nop-C", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation instruction; modifies other instructions"),
-    tInstLibEntry<tMethod>("nop-D", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation instruction; modifies other instructions"),
-    tInstLibEntry<tMethod>("nop-E", &cHardwareExperimental::Inst_Nop, 0, "No-operation instruction; modifies other instructions"),
-    tInstLibEntry<tMethod>("nop-F", &cHardwareExperimental::Inst_Nop, 0, "No-operation instruction; modifies other instructions"),
+    tInstLibEntry<tMethod>("nop-A", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation; modifies other instructions"),
+    tInstLibEntry<tMethod>("nop-B", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation; modifies other instructions"),
+    tInstLibEntry<tMethod>("nop-C", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation; modifies other instructions"),
+    tInstLibEntry<tMethod>("nop-D", &cHardwareExperimental::Inst_Nop, (nInstFlag::DEFAULT | nInstFlag::NOP), "No-operation; modifies other instructions"),
     
     tInstLibEntry<tMethod>("NULL", &cHardwareExperimental::Inst_Nop, 0, "True no-operation instruction: does nothing"),
     tInstLibEntry<tMethod>("nop-X", &cHardwareExperimental::Inst_Nop, 0, "True no-operation instruction: does nothing"),
@@ -122,6 +120,7 @@
     tInstLibEntry<tMethod>("get-head", &cHardwareExperimental::Inst_GetHead, nInstFlag::DEFAULT, "Copy the position of the ?IP? head into CX"),
     tInstLibEntry<tMethod>("if-label", &cHardwareExperimental::Inst_IfLabel, nInstFlag::DEFAULT, "Execute next if we copied complement of attached label"),
     tInstLibEntry<tMethod>("set-flow", &cHardwareExperimental::Inst_SetFlow, nInstFlag::DEFAULT, "Set flow-head to position in ?CX?"),
+    tInstLibEntry<tMethod>("goto", &cHardwareExperimental::Inst_Goto, 0, "Move IP to labeled position matching the label that follows")
   };
   
   const int n_size = sizeof(s_n_array)/sizeof(cNOPEntry);
@@ -437,7 +436,7 @@
 
 
 
-cHeadCPU cHardwareExperimental::FindLabelStart()
+cHeadCPU cHardwareExperimental::FindLabelStart(bool mark_executed)
 {
   cHeadCPU& ip = IP();
   const cCodeLabel& search_label = GetLabel();
@@ -445,7 +444,7 @@
   // Make sure the label is of size > 0.
   if (search_label.GetSize() == 0) return ip;
 
-  const cGenome& memory = ip.GetMemory();
+  cCPUMemory& memory = ip.GetMemory();
   int pos = 0;
   
   while (pos < memory.GetSize()) {
@@ -465,6 +464,12 @@
       // Check that the label matches and has examined the full sequence of nops following the 'label' instruction
       if (size_matched == search_label.GetSize()) {
         // Return Head pointed at last NOP of label sequence
+        if (mark_executed) {
+          size_matched++; // Increment size matched so that it includes the label instruction
+          const int start = pos - size_matched;
+          const int max = m_world->GetConfig().MAX_LABEL_EXE_SIZE.Get() + 1; // Max label + 1 for the label instruction itself
+          for (int i = 0; i < size_matched && i < max; i++) memory.SetFlagExecuted(start + i);
+        }
         return cHeadCPU(this, pos - 1, ip.GetMemSpace());
       }
       
@@ -478,6 +483,57 @@
 }
 
 
+cHeadCPU cHardwareExperimental::FindLabelForward(bool mark_executed)
+{
+  cHeadCPU& ip = IP();
+  const cCodeLabel& search_label = GetLabel();
+  
+  // Make sure the label is of size > 0.
+  if (search_label.GetSize() == 0) return ip;
+  
+  cHeadCPU pos(ip);
+  pos++;
+  
+  while (pos.GetPosition() != ip.GetPosition()) {
+    if (m_inst_set->IsLabel(pos.GetInst())) { // starting label found
+      const int label_start = pos.GetPosition();
+      pos++;
+      
+      // Check for direct matched label pattern, can be substring of 'label'ed target
+      // - must match all NOPs in search_label
+      // - extra NOPs in 'label'ed target are ignored
+      int size_matched = 0;
+      while (size_matched < search_label.GetSize() && pos.GetPosition() != ip.GetPosition()) {
+        if (!m_inst_set->IsNop(pos.GetInst()) || search_label[size_matched] != m_inst_set->GetNopMod(pos.GetInst())) break;
+        size_matched++;
+        pos++;
+      }
+      
+      // Check that the label matches and has examined the full sequence of nops following the 'label' instruction
+      if (size_matched == search_label.GetSize()) {
+        pos--;
+        const int found_pos = pos.GetPosition();
+
+        if (mark_executed) {
+          pos.Set(label_start);
+          const int max = m_world->GetConfig().MAX_LABEL_EXE_SIZE.Get() + 1; // Max label + 1 for the label instruction itself
+          for (int i = 0; i < max; i++) pos.SetFlagExecuted();          
+        }
+
+        // Return Head pointed at last NOP of label sequence
+        return cHeadCPU(this, found_pos, ip.GetMemSpace());
+      }
+      
+      continue; 
+    }
+    pos++;
+  }
+  
+  // Return start point if not found
+  return ip;
+}
+
+
 bool cHardwareExperimental::InjectHost(const cCodeLabel & in_label, const cGenome & injection)
 {
   // Make sure the genome will be below max size after injection.
@@ -1131,19 +1187,27 @@
 {
   ReadLabel();
   GetLabel().Rotate(1, NUM_NOPS);
-  cHeadCPU found_pos = FindLabelStart();
+  cHeadCPU found_pos = FindLabelStart(true);
   const int search_size = found_pos.GetPosition() - IP().GetPosition();
   GetRegister(REG_BX) = search_size;
   GetRegister(REG_CX) = GetLabel().GetSize();
   GetHead(nHardware::HEAD_FLOW).Set(found_pos);
   GetHead(nHardware::HEAD_FLOW).Advance();
-  return true; 
+  return true;
 }
 
 bool cHardwareExperimental::Inst_SetFlow(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_CX);
   GetHead(nHardware::HEAD_FLOW).Set(GetRegister(reg_used));
-return true; 
+  return true; 
 }
 
+bool cHardwareExperimental::Inst_Goto(cAvidaContext& ctx)
+{
+  ReadLabel();
+  GetLabel().Rotate(1, NUM_NOPS);
+  cHeadCPU found_pos = FindLabelForward(true);
+  IP().Set(found_pos);
+  return true;
+}

Modified: development/source/cpu/cHardwareExperimental.h
===================================================================
--- development/source/cpu/cHardwareExperimental.h	2007-03-04 03:40:14 UTC (rev 1382)
+++ development/source/cpu/cHardwareExperimental.h	2007-03-05 03:02:22 UTC (rev 1383)
@@ -163,7 +163,8 @@
   const cCodeLabel& GetLabel() const { return m_threads[m_cur_thread].next_label; }
   cCodeLabel& GetLabel() { return m_threads[m_cur_thread].next_label; }
   void ReadLabel(int max_size=nHardware::MAX_LABEL_SIZE);
-  cHeadCPU FindLabelStart();
+  cHeadCPU FindLabelStart(bool mark_executed);
+  cHeadCPU FindLabelForward(bool mark_executed);
   bool& ReadingLabel() { return m_threads[m_cur_thread].reading; }
   const cCodeLabel& GetReadLabel() const { return m_threads[m_cur_thread].read_label; }
   cCodeLabel& GetReadLabel() { return m_threads[m_cur_thread].read_label; }
@@ -314,6 +315,7 @@
   bool Inst_HeadCopy(cAvidaContext& ctx);
   bool Inst_HeadSearch(cAvidaContext& ctx);
   bool Inst_SetFlow(cAvidaContext& ctx);
+  bool Inst_Goto(cAvidaContext& ctx);
 };
 
 

Modified: development/source/cpu/cHeadCPU.cc
===================================================================
--- development/source/cpu/cHeadCPU.cc	2007-03-04 03:40:14 UTC (rev 1382)
+++ development/source/cpu/cHeadCPU.cc	2007-03-05 03:02:22 UTC (rev 1383)
@@ -44,205 +44,10 @@
   if (m_position >= 0 && m_position < mem_size) return;
   
   // If the memory is gone, just stick it at the begining of its parent.
+  // @DMB - note: this violates the circularity of memory spaces.  You can loop forward, but not backward.
   if (mem_size == 0 || m_position < 0) m_position = 0;
   
   // position back at the begining of the creature as necessary.
   m_position %= GetMemory().GetSize();
 }
 
-
-/*
- FindLabel(direction)
-
- Search in 'direction' (+ or - 1) from this head for the compliment of
- the label in 'next_label' and return a head to the resulting pos.
-*/
-cHeadCPU cHeadCPU::FindLabel(const cCodeLabel& label, int direction)
-{
-  // Make sure the label is of size > 0.
-  if (label.GetSize() == 0) return *this;
-  
-  int found_pos = -1;
-  
-  // Call special functions depending on if jump is forwards or backwards.
-  if (direction < 0) {
-    found_pos = FindLabel_Backward(label, GetMemory(), m_position - label.GetSize());
-  } else {
-    found_pos = FindLabel_Forward(label, GetMemory(), m_position);
-  }
-  
-  if (found_pos >= 0) {
-    // Return the last line of the found label, or the starting point.
-    cHeadCPU search_head(*this);
-    search_head.Set(found_pos - 1);
-    return search_head;
-  }
-  
-  // It wasn't found; return the starting position of the instruction pointer.
-  return *this;
-}
-
-/*
- FindLabel_Forward
-
- Search forwards for search_label from _after_ position pos in the
- memory.  Return the first line _after_ the the found label.  It is okay
- to find search label's match inside another label.
-*/
-int cHeadCPU::FindLabel_Forward(const cCodeLabel& search_label, const cGenome& search_mem, int pos)
-{ 
-  assert(pos < search_mem.GetSize() && pos >= 0);
-  
-  int search_start = pos;
-  int label_size = search_label.GetSize();
-  bool found_label = false;
-  const cInstSet& inst_set = m_hardware->GetInstSet();
-  
-  // Move off the template we are on.
-  pos += label_size;
-  
-  // Search until we find the complement or exit the memory.
-  while (pos < search_mem.GetSize()) {
-    
-    // If we are within a label, rewind to the beginning of it and see if
-    // it has the proper sub-label that we're looking for.
-    
-    if (inst_set.IsNop(search_mem[pos])) {
-      // Find the start and end of the label we're in the middle of.
-      
-      int start_pos = pos;
-      int end_pos = pos + 1;
-      while (start_pos > search_start &&
-             inst_set.IsNop( search_mem[start_pos - 1] )) {
-        start_pos--;
-      }
-      while (end_pos < search_mem.GetSize() &&
-             inst_set.IsNop( search_mem[end_pos] )) {
-        end_pos++;
-      }
-      int test_size = end_pos - start_pos;
-      
-      // See if this label has the proper sub-label within it.
-      int max_offset = test_size - label_size + 1;
-      for (int offset = start_pos; offset < start_pos + max_offset; offset++) {
-        
-        // Test the number of matches for this offset.
-        int matches;
-        for (matches = 0; matches < label_size; matches++) {
-          if (search_label[matches] !=
-              inst_set.GetNopMod( search_mem[offset + matches] )) {
-            break;
-          }
-        }
-        
-        // If we have found it, break out of this loop!
-        if (matches == label_size) {
-          found_label = true;
-          break;
-        }
-      }
-      
-      // If we've found the complement label, set the position to the end of
-      // the label we found it in, and break out.
-      
-      if (found_label == true) {
-        pos = end_pos;
-        break;
-      }
-      
-      // We haven't found it; jump pos to just after the current label being
-      // checked.
-      pos = end_pos;
-    }
-    
-    // Jump up a block to the next possible point to find a label,
-    pos += label_size;
-  }
-  
-  // If the label was not found return a -1.
-  if (found_label == false) pos = -1;
-  
-  return pos;
-}
-
-
-/*
- FindLabel_Backward
- 
- Search backwards for search_label from _before_ position pos in the
- memory.  Return the first line _after_ the the found label.  It is okay
- to find search label's match inside another label.
-*/
-int cHeadCPU::FindLabel_Backward(const cCodeLabel& search_label, const cGenome& search_mem, int pos)
-{ 
-  assert(pos < search_mem.GetSize());
-  
-  int search_start = pos;
-  int label_size = search_label.GetSize();
-  bool found_label = false;
-  const cInstSet& inst_set = m_hardware->GetInstSet();
-  
-  // Move off the template we are on.
-  pos -= label_size;
-  
-  // Search until we find the complement or exit the memory.
-  while (pos >= 0) {
-    // If we are within a label, rewind to the beginning of it and see if
-    // it has the proper sub-label that we're looking for.
-    
-    if (inst_set.IsNop( search_mem[pos] )) {
-      // Find the start and end of the label we're in the middle of.
-      
-      int start_pos = pos;
-      int end_pos = pos + 1;
-      while (start_pos > 0 && inst_set.IsNop(search_mem[start_pos - 1])) {
-        start_pos--;
-      }
-      while (end_pos < search_start &&
-             inst_set.IsNop( search_mem[end_pos] )) {
-        end_pos++;
-      }
-      int test_size = end_pos - start_pos;
-      
-      // See if this label has the proper sub-label within it.
-      int max_offset = test_size - label_size + 1;
-      for (int offset = start_pos; offset < start_pos + max_offset; offset++) {
-        
-        // Test the number of matches for this offset.
-        int matches;
-        for (matches = 0; matches < label_size; matches++) {
-          if (search_label[matches] !=
-              inst_set.GetNopMod( search_mem[offset + matches] )) {
-            break;
-          }
-        }
-        
-        // If we have found it, break out of this loop!
-        if (matches == label_size) {
-          found_label = true;
-          break;
-        }
-      }
-      
-      // If we've found the complement label, set the position to the end of
-      // the label we found it in, and break out.
-      
-      if (found_label == true) {
-        pos = end_pos;
-        break;
-      }
-      
-      // We haven't found it; jump pos to just before the current label
-      // being checked.
-      pos = start_pos - 1;
-    }
-    
-    // Jump up a block to the next possible point to find a label,
-    pos -= label_size;
-  }
-  
-  // If the label was not found return a -1.
-  if (found_label == false) pos = -1;
-  
-  return pos;
-}

Modified: development/source/cpu/cHeadCPU.h
===================================================================
--- development/source/cpu/cHeadCPU.h	2007-03-04 03:40:14 UTC (rev 1382)
+++ development/source/cpu/cHeadCPU.h	2007-03-05 03:02:22 UTC (rev 1383)
@@ -85,7 +85,6 @@
   
   inline void Advance() { m_position++; Adjust(); }
   inline void Retreat() { m_position--; Adjust(); }
-  cHeadCPU FindLabel(const cCodeLabel& label, int direction = 1);
 
   inline const cInstruction& GetInst() const { return GetMemory()[m_position]; }
   inline const cInstruction& GetInst(int offset) const { return GetMemory()[m_position + offset]; }

Modified: development/support/config/instset-experimental.cfg
===================================================================
--- development/support/config/instset-experimental.cfg	2007-03-04 03:40:14 UTC (rev 1382)
+++ development/support/config/instset-experimental.cfg	2007-03-05 03:02:22 UTC (rev 1383)
@@ -28,6 +28,8 @@
 set-flow   1   # A   Move flow-head to address in ?CX?
 label      1   # B   delineate labeled regions of the genome
 
+#goto       1   # C   Move IP to the labeled position in the genome
+
 #adv-head   1
 #jump-f     1
 #jump-b     1




More information about the Avida-cvs mailing list