Add to git.
[dlife.git] / cell.h
1 /* DLIFE Copyright (C) 2000 Richard W.M. Jones <rich@annexia.org>
2  * and other authors listed in the ``AUTHORS'' file.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * $Id: cell.h,v 1.1 2002/04/05 14:40:25 rich Exp $
19  */
20
21 #ifndef cell_h
22 #define cell_h
23
24 #include "config.h"
25
26 #include <stdio.h>
27
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>          /* For MAXHOSTNAMELEN. */
30 #endif
31
32 #include "types.h"
33 #include "dlink.h"
34 #include "state.h"
35 #include "crc.h"
36
37 struct soup_frag;
38
39 #define STACK_SIZE 16
40
41 /* These two parameters define the maximum cell size. The first parameter
42  * is the maximum length of searches permitted by the FINDB/FINDF
43  * commands. It stops those commands from running wild if a label is
44  * missing. The second one limits the maximum cell size that may be
45  * allocated by MALLOC - and hence directly the maximum cell size
46  * overall. There is also a minimum cell size, although we keep this
47  * small.
48  */
49 #define FIND_LENGTH_LIMIT 512
50 #define MAX_CELL_SIZE     512
51 #define MIN_CELL_SIZE      10
52
53 /* The maximum number of errors that a cell may get before we
54  * automatically blow it away (even if the soup isn't full and
55  * doesn't need reaping). This prevents us wasting valuable CPU
56  * time on cells which have gone completely AWOL.
57  */
58 #define MAX_ERRORS        100
59
60 enum cell_state
61 {
62   state_exec,
63   state_find
64 };
65
66 struct cell
67 {
68   struct cell *next;            /* Alive cells are linked thru this list. */
69   struct cell *prev;            /* Alive cells are linked thru this list. */
70   struct cell *worse;           /* Cells are kept in error order on this list*/
71   struct cell *better;          /* Cells are kept in error order on this list*/
72   reg_t a, b, i, p;             /* Registers. */
73   reg_t stack[STACK_SIZE];      /* Stack. */
74   int sp;                       /* Stack pointer. */
75   int errors;                   /* Cumulative number of errors encountered */
76   enum cell_state state;        /* Executing or finding. */
77   int dir;                      /* [Find] -1 = back, 1 = forwards */
78   int plen;                     /* [Find] length of pattern */
79   int lim;                      /* [Find] search limit (counts down to 0) */
80   struct soup_frag *frag;       /* Fragment in the soup */
81   struct cell *daughter;        /* Daughter (after MALLOC, before DIVIDE) */
82   unsigned long long cycle;     /* Number of cycles of runtime. */
83   crc_t crc;                    /* CRC at birth (after mother's DIVIDE). */
84   crc_t mother_crc;             /* CRC of mother. */
85   addr_t fbase;                 /* Used when saving/loading soup image only. */
86 };
87
88 extern int verbose;
89
90 extern struct cell *cell_malloc (struct state *state,
91                                  const struct cell *mother,
92                                  struct soup_frag *daughter_frag);
93 extern void cell_divide (struct state *state,
94                          const struct cell *mother,
95                          struct cell *daughter);
96 extern void cell_activate (struct state *state,
97                            struct cell *cell);
98 extern void cell_kill (struct state *state,
99                        struct cell *cell);
100
101 extern void cell_state_load (struct state *state, FILE *fp);
102 extern void cell_state_save (struct state *state, FILE *fp);
103
104 extern void cell_check (struct state *state);
105
106 extern void exec_insn (struct state *state, struct cell *cell);
107
108 extern void run_thread (struct state *state);
109
110 /* This macro is used to increment the error count in a cell. It
111  * ensures that the cell ``worst-to-best'' list is kept up to date.
112  */
113 #define INC_ERRORS(state,cell) do { (cell)->errors++; if ((cell)->worse && (cell)->errors > (cell)->worse->errors) _cell_move_worse ((state), (cell)); } while (0)
114
115 extern void _cell_move_worse (struct state *state, struct cell *cell);
116
117 extern struct soup_frag *_cell_get_any_frag (struct state *state);
118
119 extern void _cell_fixup_soup_ref (struct state *state,
120                                   addr_t fbase, struct soup_frag *frag);
121
122 #endif /* cell_h */