📄 instruct.c
字号:
}/* void movdd(ce) *(is.dreg) = is.sval + flaw(ce); * is.dreg = destination register, where moved value will be placed * is.sval = value to be placed in the dest reg * is.dmod = value by which to modulus destination register * is.dran = range within which to contain destination register */void movdd(ce) /* inter-register mov */Pcells ce;{ *(is.dreg) = is.sval + flaw(ce); if (is.dmod) { *(is.dreg) = mo(*(is.dreg), is.dmod); is.dmod = 0; } else if (is.dran && (*(is.dreg) > is.dran || *(is.dreg) < -is.dran)) { *(is.dreg) = 0; is.dran = 0; } ce->c.fl = 0;}/* void movdi(ce) is.dins->inst = is.sval + flaw(ce); * is.dval = address of destination instruction * is.dins = pointer to destination instruction * is.sval = value to be moved to destination instruction * is.sval2 = original value of destination instruction */void movdi(ce) /* move from register to soup, e.g.: soup [R0] = R1 */Pcells ce;{ if ((0 <= is.dval && is.dval < SoupSize)#ifdef WRITEPROT && (!is.dins->write || IsInsideCell(ce, is.dval))#endif /* WRITEPROT */ ) { is.dins->inst = is.sval + flaw(ce); ce->c.fl = 0; if (is.dval >= ce->md.p && is.dval < ce->md.p + ce->md.s) ce->d.mov_daught++; else if (is.dins->inst != is.sval2) MutBookeep(is.dval); } else SetFlag(ce);}/* void movid(ce) *(is.dreg) = is.sins->inst + flaw(ce); * is.sins = pointer to source instruction * is.sval = address of source instruction * is.dreg = destination register, where moved value will be placed * is.dmod = value by which to modulus destination register * is.dran = range within which to contain destination register */void movid(ce) /* move from soup to register, e.g.: R0 = soup [R1] */Pcells ce;{#ifdef READPROT if (!is.sins->read || IsInsideCell(ce, is.sval))#endif /* READPROT */ { *(is.dreg) = is.sins->inst + flaw(ce); ce->c.fl = 0; }#ifdef READPROT else { SetFlag(ce); return; }#endif /* READPROT */ if (is.dmod) { *(is.dreg) = mo(*(is.dreg), is.dmod); is.dmod = 0; } else if (is.dran && (*(is.dreg) > is.dran || *(is.dreg) < -is.dran)) { *(is.dreg) = 0; is.dran = 0; }}/* void movii(ce) is.dins->inst = is.sins->inst; * is.dval = address of destination instruction * is.dins = pointer to destination instruction * is.sval = address of source instruction * is.sins = pointer to source instruction * is.dtra = track of destination instruction * is.sval2 = original value of destination instruction */void movii(ce) /* move data from soup to soup, e.g.: soup [R0] = soup [R1] */Pcells ce;{ if ((is.dval != is.sval)#ifdef WRITEPROT && (!is.dins->write || IsInsideCell(ce, is.dval))#endif /* WRITEPROT */#ifdef READPROT && (!is.sins->read || IsInsideCell(ce, is.sval))#endif /* READPROT */ && (0 <= is.dval && is.dval < SoupSize) && (0 <= is.sval && is.sval < SoupSize)) { is.dins->inst = is.sins->inst; if (RateMovMut && ++CountMovMut >= RateMovMut) { mut_site(soup + ad(is.dval), is.dtra); CountMovMut = tlrand() % RateMovMut; TotMovMut++; } if (WatchMov) GenExMov(ce, is.dval, is.sval); if (is.dval >= ce->md.p && is.dval < ce->md.p + ce->md.s) ce->d.mov_daught++; else if (is.dins->inst != is.sval2) MutBookeep(is.dval); ce->c.fl = 0;#ifdef ALCOMM if ( MIsDFEnabled( TrtMVEvent ) ) TMoveD( ce->mm.p,is.sval,is.dval);#endif /* ALCOMM */ } else SetFlag(ce);}/* void adr(ce) find address of a template * is.mode = search mode: 1 = forward, 2 = backward, 0 = outward * is.mode2 = preference: 1 = forward, 2 = backward, and return for * direction found: 1 = forward, 2 = backward, 3 = both, 0 = none * is.dval = starting address for forward search * and return for finish address * is.dval2 = starting address for backward search * and return for finish address * is.dreg = destination register where target address will be stored * is.dreg2 = destination register where template size will be stored * is.dreg3 = destination register where offset of target will be stored * is.sval = return address if template size = 0 * is.sval2 = template size, 0 = no template * is.sval3 = search limit, and return for distance actually searched * is.dmod = modulus value for is.dreg * is.dmod2 = modulus value for is.dreg2 * is.dmod3 = modulus value for is.dreg3 * is.dran = range to maintain for is.dreg * is.dran2 = range to maintain for is.dreg2 * is.dran3 = range to maintain for is.dreg3 */void adr(ce) /* find address of a template */Pcells ce;{ I32s adrt; if (!is.sval2) { *(is.dreg) = is.sval; /* source template missing *//* SetFlag(ce); */ return; } if (!is.mode) /* outward search */ adrt = ctemplate(&is.dval, &is.dval2, &is.sval3, &is.mode2, is.sval2, 'o', ce); else if (is.mode == 1) /* forward search */ adrt = ctemplate(&is.dval, &is.dval2, &is.sval3, &is.mode2, is.sval2, 'f', ce); else if (is.mode == 2) /* backward search */ adrt = ctemplate(&is.dval, &is.dval2, &is.sval3, &is.mode2, is.sval2, 'b', ce); if (adrt < 0) /* target template not found */ { is.iip = is.sval2 + 1; /* skip IP over source template */ SetFlag(ce); return; } *(is.dreg3) = is.sval3; if (is.dmod3) { *(is.dreg3) = mo(*(is.dreg3), is.dmod3); is.dmod3 = 0; } else if (is.dran3 && (*(is.dreg3) > is.dran3 || *(is.dreg3) < -is.dran3)) { *(is.dreg3) = 0; is.dran3 = 0; } *(is.dreg2) = is.sval2; if (is.dmod2) { *(is.dreg2) = mo(*(is.dreg2), is.dmod2); is.dmod2 = 0; } else if (is.dran2 && (*(is.dreg2) > is.dran2 || *(is.dreg2) < -is.dran2)) { *(is.dreg2) = 0; is.dran2 = 0; } *(is.dreg) = adrt; if (is.dmod) { *(is.dreg) = mo(*(is.dreg), is.dmod); is.dmod = 0; } else if (is.dran && (*(is.dreg) > is.dran || *(is.dreg) < -is.dran)) { *(is.dreg) = 0; is.dran = 0; } ce->c.fl = 0; return;}/* void malchm(ce) allocate space and protect it * is.dreg = destination register where allocated address is stored * is.sval = requested size of block for mal() * is.sval2 = flawed size of block * is.sval3 = suggested address, and allocated address * is.mode = memory protection mode (rwx), probably MemModeProt * is.mode2 = memory allocation mode for mal() */void malchm(ce) /* allocate space and protect it */Pcells ce;{ /* is.sval = requested size of block, is.sval2 = flawed size of block */ /* is.sval3 = suggested address, & returned address */ /* is.dreg = location of block, is.mode2 = allocation style */ if (!(is.sval2 = mal(ce,&is.sval3,is.sval,is.mode2))) { SetFlag(ce); return ; } *(is.dreg) = is.sval3; /* is.sval3 = location of chmoded block */ /* is.sval2 = size of chmoded block */ /* is.mode = chmod mode, unix notation, e.g. 7 = full protection */ if (chmode(ce,is.sval3,is.sval2,is.mode)) /* could be MemModeProt */ SetFlag(ce); else ce->c.fl = 0;#ifdef HSEX if (DoMate()) SetXover(ce); else ce->d.x_over_addr = ce->d.mate_addr = 0;#endif /* ifdef HSEX */}/* void divide(ce) cell division * is.sval = offset of IP into daughter's genome * is.sval2 = eject genome from soup = 0, 1 = leave in soup * is.mode = divide mode (3 steps) */void divide(ce) /* cell division */Pcells ce;{ Pcells nc; /* pointer to the new cell */ I32s i, j, found = 0; CellInd ni; if (ce->md.s < MinCellSize || ce->d.mov_daught < (I32s) (ce->md.s * MovPropThrDiv)) { SetFlag(ce); return; } if (DivSameSiz) { if (ce->mm.s != ce->md.s) { SetFlag(ce); return; } if (DivSameGen && !IsSameGen(ce->mm.s, soup + ce->md.p, soup + ce->mm.p)) { SetFlag(ce); return; } } if (is.sval2) switch (is.mode) { case 0: /* create cpu */ { if ((ce->d.ne.a == ce->q.this.a) && (ce->d.ne.i == ce->q.this.i)) /* if there is no cpu (first call to div 0) */ { nc = GetFreeCell(); nc->ld = 1; nc->mm = ce->md; nc->d.dm = 1; nc->d.genome = soup + nc->mm.p; ce->d.ne = nc->q.this; nc->c.ip = nc->mm.p;#if INST == 2 nc->c.ip += (is.sval % nc->mm.s); for (i = 0; i < NUMREG; i++) nc->c.re[i] = ce->c.re[i];#endif /* INST == 2 */ } else /* if there is a cpu (second call to div 0) */ { nc = &cells[ce->d.ne.a][ce->d.ne.i]; if (nc->d.is) /* call to div 0 after call to div 1 */ { RmvFrmSlicer(nc); nc->d.is = 0; } else /* two sequential calls to div 0, error */ { SetFlag(ce); return; } } break; } case 1: /* start cpu */ { if ((ce->d.ne.a == ce->q.this.a) && (ce->d.ne.i == ce->q.this.i)) /* if there is no cpu, div 1 before div 0 */ { nc = GetFreeCell(); nc->ld = 1; nc->mm = ce->md; nc->d.dm = 1; nc->d.genome = soup + nc->mm.p; ce->d.ne = nc->q.this; nc->c.ip = nc->mm.p;#if INST == 2 nc->c.ip += (is.sval % nc->mm.s); for (i = 0; i < NUMREG; i++) nc->c.re[i] = ce->c.re[i];#endif /* INST == 2 */ } else /* if there is already a cpu, make pointers to it */ nc = &cells[ce->d.ne.a][ce->d.ne.i]; if (nc->d.is) /* 2nd call to div 1, cpu is already started */ { RmvFrmSlicer(nc); nc->d.is = 0; } else /* not 2nd call to div 1, cpu is not already started */ { EntBotSlicer(nc); nc->d.is = 1; } break; } case 2: /* split cells */ { if ((ce->d.ne.a == ce->q.this.a) && (ce->d.ne.i == ce->q.this.i)) /* if there is no cpu, div 2 before div 0 */ { nc = GetFreeCell(); nc->ld = 1; nc->mm = ce->md; nc->d.genome = soup + nc->mm.p; nc->c.ip = nc->mm.p;#if INST == 2 nc->c.ip += (is.sval % nc->mm.s); for (i = 0; i < NUMREG; i++) nc->c.re[i] = ce->c.re[i];#endif /* INST == 2 */ } else nc = &cells[ce->d.ne.a][ce->d.ne.i]; if (!nc->d.is) /* no slicer, div 2 before div 1 */ { EntBotSlicer(nc); nc->d.is = 1; } ce->md.s = ce->md.p = 0; ce->d.ne = ce->q.this; /* clean up if div 0 or 1 before 2 */ nc->d.dm = 0; EntBotReaper(nc); DownReperIf(ce); DivideBookeep(ce, nc); } } /* switch */#if INST != 1 else Emigrate(ce);#endif /* INST != 1 */ ce->c.fl = 0;}#if INST != 1void Emigrate(ce)Pcells ce;{ I32s i; FpInst p = soup + ce->md.p;/* write genome to ejection function here *//* the following code erases the genome from the soup */#ifdef ERROR if (!ce->md.s) FEError(-613,EXIT,WRITE, "Tierra Emigrate() error: ce->md.s = 0");#endif /* ERROR */ for (i = 0; i < ce->md.s; i++) p[i].inst = 0;#ifdef ERROR if (ce->md.p < 0 || ce->md.p >= SoupSize) FEError(-613,EXIT,WRITE, "Tierra Emigrate() error: ce->md.p not in soup");#endif /* ERROR */ chmode(ce, ce->md.p, ce->md.s, MemModeFree); MemDealloc(ce->md.p, ce->md.s); ce->d.mov_daught = 0; ce->d.fecundity++; ce->md.s = 0;}#endif /* INST != 1 */Pcells GetFreeCell(){ Pcells fc; I32s i, j, found = 0; if (++NumCells > CellsSize - 2) CheckCells(); for (i = 0; i < NumCelAr; i++) /* find unoccupied cell struct */ { for (j = 0; j < CelArSiz; j++) {#ifdef ERROR if (i * j >= CellsSize) FEError(-502,EXIT,WRITE, "Tierra GetFreeCell() error, exiting");#endif if(!cells[i][j].ld) { found = 1; fc = &cells[i][j]; break; } } if (found) break; } InitCell(i, j, fc); return fc;}void CheckCells() /* check and adjust memory allocation if necessary */{ I32s j, oCellsSize = CellsSize; Pcells Fp tcells;#ifdef ERROR sprintf(mes[0], "in_div CheckCells: recalloc, NumCells = %ld", NumCells); sprintf(Buff, " old CellsSize = %ld ", CellsSize);#endif NumCelAr++; tcells = (Pcells Fp) trecalloc((I8s Fp) cells, (I32u) sizeof(Pcells) * (I32u) NumCelAr, (I32u) sizeof(Pcells) * (I32u) (NumCelAr - 1)); if (tcells) cells = tcells; else if (cells) { tfree(cells); cells = NULL; FEError(-503,EXIT,WRITE, "Tierra CheckCells() cells trecalloc error, out of memory, exiting"); } CellsSize = NumCelAr * CelArSiz; cells[NumCelAr - 1] = (Pcells) tcalloc(CelArSiz, sizeof(Cell)); if (cells[NumCelAr - 1] == NULL) { FEError(-504,EXIT,WRITE, "Tierra CheckCells() cells[] tcalloc error, out of memory, exiting"); }#ifdef ERROR sprintf(mes[1], "%s new CellsSize = %ld", Buff, CellsSize); FEMessage(2,mes);#ifdef __TURBOC__ sprintf(mes[0], "coreleft = %lu divide (cells)", coreleft()); FEMessage(1,mes);#endif#endif for (j = 0; j < CelArSiz; j++) InitCell(NumCelAr - 1, j, &cells[NumCelAr - 1][j]);}I32s flaw(ce)Pcells ce;{ CountFlaw++; if (RateFlaw && CountFlaw >= RateFlaw) { CountFlaw = tlrand() % RateFlaw; TotFlaw++; ce->d.flaw++; if (tcrand() % 2) return 1; return -1; } return 0;}/* search in specified direction for * nop template return address, returns address of instruction following * target template, i.e., target + tz * NOTE: ce->c.ip must point to the instruction (agent) being executed */I32s ctemplate(f, b, slim, mode, tz, dir, ce)I32s *f; /* starting address for forward search */I32s *b; /* starting address for backward search */I32s *slim; /* search limit, and return for distance searched */I8s *mode; /* preference for forward (1) or backward (2) target */I32s tz; /* template size */I32s dir; /* direction of search, f = forward, b = backward, o = out */Pcells ce; /* which cell */{ I32s o, l = 1, adrt; I32s i = 0, fmatch = 0, bmatch = 0; I8s df, db; Pgl tgl; if ((tz < MinTemplSize) || (tz > SoupSize)) { adrt = -1; *mode = 0; goto finish; } if ((I8s) dir == 'o') /* both directions */ df = db = 1; else if ((I8s) dir == 'f') /* forward only */ { df = 1; db = 0; } else if ((I8s) dir == 'b') /* backwards only */ { df = 0; db = 1; } o = ad(ce->c.ip + 1); while (1) { while (1) /* this skips sections of codes that are not templates (NOPs) */ {#if PLOIDY == 1 if ((df && /* forward */ (soup[*f].inst == Nop0 || soup[*f].inst == Nop1)#ifdef READPROT && (!soup[*f].read || IsInsideCell(ce,*f))#endif /* READPROT */ ) || (db && /* backward */ (soup[*b].inst == Nop0 || soup[*b].inst == Nop1)#ifdef READPROT && (!soup[*b].read || IsInsideCell(ce,*b))#endif /* READPROT */ ))#else /* PLOIDY > 1 */ if ((df && /* forward */ (soup[*f][ce->d.tr].inst == Nop0 || soup[*f][ce->d.tr].inst==Nop1)#ifdef READPROT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -