[Avida-SVN] r2148 - in branches/dkdev: Avida.xcodeproj source/cpu
dknoester at myxo.css.msu.edu
dknoester at myxo.css.msu.edu
Tue Oct 23 06:18:31 PDT 2007
Author: dknoester
Date: 2007-10-23 09:18:30 -0400 (Tue, 23 Oct 2007)
New Revision: 2148
Modified:
branches/dkdev/Avida.xcodeproj/project.pbxproj
branches/dkdev/source/cpu/cHardwareCPU.cc
branches/dkdev/source/cpu/cHardwareCPU.h
Log:
If/else/endif.
Modified: branches/dkdev/Avida.xcodeproj/project.pbxproj
===================================================================
--- branches/dkdev/Avida.xcodeproj/project.pbxproj 2007-10-22 15:03:09 UTC (rev 2147)
+++ branches/dkdev/Avida.xcodeproj/project.pbxproj 2007-10-23 13:18:30 UTC (rev 2148)
@@ -758,7 +758,7 @@
DCC315CF076253A5008F7A48 /* Makefile */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = "<group>"; };
DCC315D0076253A5008F7A48 /* task_event_gen.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = task_event_gen.cc; sourceTree = "<group>"; };
DCC315D1076253A5008F7A48 /* task_event_gen.old.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = task_event_gen.old.cc; sourceTree = "<group>"; };
- DCC3164D07626CF3008F7A48 /* avida */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = avida; sourceTree = BUILT_PRODUCTS_DIR; };
+ DCC3164D07626CF3008F7A48 /* avida */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = avida; sourceTree = BUILT_PRODUCTS_DIR; };
E03F28DA0B8A2840009966B8 /* cDeme.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cDeme.cc; sourceTree = "<group>"; };
E03F28DB0B8A2840009966B8 /* cDeme.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cDeme.h; sourceTree = "<group>"; };
E08178B90B3DCB9600B474B6 /* cTopology.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cTopology.h; sourceTree = "<group>"; };
Modified: branches/dkdev/source/cpu/cHardwareCPU.cc
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.cc 2007-10-22 15:03:09 UTC (rev 2147)
+++ branches/dkdev/source/cpu/cHardwareCPU.cc 2007-10-23 13:18:30 UTC (rev 2148)
@@ -393,7 +393,7 @@
cInstEntryCPU("if-less.end", &cHardwareCPU::Inst_IfLessEnd),
cInstEntryCPU("if-n-equ.end", &cHardwareCPU::Inst_IfNotEqualEnd),
// cInstEntryCPU("if-less.else", &cHardwareCPU::Inst_IfLessElse),
-// cInstEntryCPU("else", &cHardwareCPU::Inst_Else),
+ cInstEntryCPU("else", &cHardwareCPU::Inst_Else),
cInstEntryCPU("end-if", &cHardwareCPU::Inst_EndIf),
// cInstEntryCPU("", &cHardwareCPU::Inst_),
};
@@ -473,8 +473,7 @@
inst_cost[i] = m_inst_set->GetCost(cInstruction(i));
inst_ft_cost[i] = m_inst_set->GetFTCost(cInstruction(i));
}
-#endif
-
+#endif
}
void cHardwareCPU::cLocalThread::operator=(const cLocalThread& in_thread)
@@ -3941,37 +3940,28 @@
}
-/*! Advance the IP to the first end-if, or just one instruction if it's not present
-in the genome. Note that this is O(n), n==genome size. If this is a problem, we
-can preload the location of all end-if instructions at construction time.
-
-Not sure if this is going to be a problem, but we do loop around the genome searching
-for the end-if. In that case, there's a good probability that replication can be
-royally screwed up. Be warned...
+/*! Find the first occurence of the passed-in instruction from the IP() forward,
+wrapping around the genome as required. If the given instruction is not in the
+genome, return the starting position.
*/
-void cHardwareCPU::Advance_EndIf() {
+cHeadCPU cHardwareCPU::Find(const char* instr) {
cHeadCPU ptr(IP());
const int current = ptr.GetPosition();
ptr.Advance();
while(ptr.GetPosition() != current) {
ptr.Advance();
- if(m_inst_set->GetName(ptr.GetInst())=="end-if") {
- IP().Set(ptr);
- break;
+ if(m_inst_set->GetName(ptr.GetInst())==instr) {
+ return ptr;
}
}
- // Advance the IP one instruction - If we found an end-if, this advances to
- // the following instruction. If not, this skips over the instruction following
- // the if-less-end, behaving just like a standard Avida conditional.
- IP().Advance();
}
bool cHardwareCPU::Inst_IfLessEnd(cAvidaContext& ctx) {
const int x = FindModifiedRegister(REG_BX);
const int y = FindNextRegister(x);
-
- if(GetRegister(x) >= GetRegister(y)) { Advance_EndIf(); }
+
+ if(GetRegister(x) >= GetRegister(y)) { Else_TopHalf(); }
return true;
}
@@ -3980,23 +3970,47 @@
const int x = FindModifiedRegister(REG_BX);
const int y = FindNextRegister(x);
- if(GetRegister(x) == GetRegister(y)) { Advance_EndIf(); }
+ if(GetRegister(x) == GetRegister(y)) { Else_TopHalf(); }
return true;
}
-bool cHardwareCPU::Inst_IfLessElse(cAvidaContext& ctx) {
- // Not yet implemented...
- return true;
+void cHardwareCPU::Else_TopHalf() {
+ cHeadCPU else_head = Find("else");
+ cHeadCPU endif_head = Find("endif");
+
+ // Condition failed. If there's an else-clause, jump to it.
+ // If there isn't an else-clause, try to jump to the endif.
+ // Note that the IP is unconditionally advanced *after* this instruction
+ // has executed. If there is no else or endif, advance one instruction.
+ if(else_head.GetPosition() != IP().GetPosition()) {
+ IP().Set(else_head);
+ } else if(endif_head.GetPosition() != IP().GetPosition()) {
+ IP().Set(endif_head);
+ } else {
+ // No else or endif. Advance past the next instruction (as normal).
+ IP().Advance();
+ }
}
-bool cHardwareCPU::Inst_Else(cAvidaContext& ctx) {
+/*! The only way that this instruction can be executed is if the if passed, or
+if there was no if. In both cases, we're going to jump to the first <end-if>, or
+skip one instruction.
+*/
+bool cHardwareCPU::Inst_Else(cAvidaContext& ctx) {
+ cHeadCPU endif_head = Find("endif");
+ if(endif_head.GetPosition() != IP().GetPosition()) {
+ // If the <end-if> is somewhere else, jump to it.
+ IP().Set(endif_head);
+ } else {
+ // Otherwise, just skip one instruction.
+ IP().Advance();
+ }
return true;
}
-
+//! This is just a placeholder; it has no functionality of its own.
bool cHardwareCPU::Inst_EndIf(cAvidaContext& ctx) {
- // Flag only, for right now.
return true;
}
Modified: branches/dkdev/source/cpu/cHardwareCPU.h
===================================================================
--- branches/dkdev/source/cpu/cHardwareCPU.h 2007-10-22 15:03:09 UTC (rev 2147)
+++ branches/dkdev/source/cpu/cHardwareCPU.h 2007-10-23 13:18:30 UTC (rev 2148)
@@ -507,12 +507,13 @@
bool Inst_EnvX(cAvidaContext& ctx);
bool Inst_EnvY(cAvidaContext& ctx);
bool Inst_Sorted(cAvidaContext& ctx);
-
+
+
// If-less-else-if, else, endif
- void Advance_EndIf();
+ cHeadCPU Find(const char* instr);
+ void Else_TopHalf();
bool Inst_IfLessEnd(cAvidaContext& ctx);
bool Inst_IfNotEqualEnd(cAvidaContext& ctx);
- bool Inst_IfLessElse(cAvidaContext& ctx);
bool Inst_Else(cAvidaContext& ctx);
bool Inst_EndIf(cAvidaContext& ctx);
};
More information about the Avida-cvs
mailing list