void bytecode2Source( ByteCode& bc, string& src, InstructionSet& iset ) { src.clear(); for (unsigned i=0;i<bc.size();i++) src += iset.getName(bc[i]) + "/"; src += "."; }
ByteCode_Type instruction2ByteCode( string inst, InstructionSet& iset ) { for (ByteCode_Type i=0;i<iset.size();i++) if (inst == iset.getName(i) ) return i; throw (string)"Instruction not recognized: " + inst; }
// Runs a given ByteCode, returns true if timed-out. bool runByteCode(InstructionSet& iset, MemCore& core, ByteCode& bc, long randseed, long max_rtime, int max_loop_depth) { if (!max_rtime) max_rtime = 3600*24*7; // that's a week's worth of runtime! core.C = &bc; core.c = 0; iset.clear(); #ifndef DEBUG /* Setup the time-out handler */ timedout=false; signal(SIGALRM, alarm_handler); alarm(max_rtime); #endif iset.setMaxLoopDepth(max_loop_depth); try { do { #ifdef DEBUG cout << " [I]=" << core.I << ", [F]=" << core.F << ", D[I]=" << core.D[core.I] << endl; cout << " Next instruction: " << iset.getName((*core.C)[core.c]) << ". (hit enter)"; cin.get(); cout << endl; #endif iset.exec((*core.C)[core.c], core); core.c++; } while ( (core.c<(*core.C).size()) && (!timedout) ); } catch(int whatever) { return true; // program failed } #ifndef DEBUG alarm(0); // turns off alarm #endif if (timedout) return true; // interpreter timed-out else return false; } // runByteCode