aboutsummaryrefslogtreecommitdiff
path: root/src/process.c
blob: 8d500aef55793e0f168fd1468a86744dc2c6a8d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "getter.h"
#include "instset.h"
#include "memory.h"
#include "evolver.h"
#include "common.h"
#include "process.h"

static boolean g_is_init;
static uint32 g_count;
static uint32 g_capacity;
static uint32 g_first;
static uint32 g_last;
static uint32 g_instructions_executed;
static Process *g_procs;

void _sal_proc_init(void)
{
	/* Initialize process module to its initial state. We initialize the reaper
	queue with a capacity of 1. 'First' and 'last' organism pointers are
	initialized to (uint32)-1 (to indicate they point to no organism, as no
	organism exists yet).
	*/
	assert(!g_is_init);
	g_is_init = TRUE;
	g_capacity = 1;
	g_first = UINT32_MAX;
	g_last = UINT32_MAX;
	g_procs = calloc(g_capacity, sizeof(Process));
	assert(g_procs);
}

void _sal_proc_quit(void)
{
	/* Reset process module back to zero; free up the process queue.
	*/
	assert(g_is_init);
	free(g_procs);
	g_is_init = FALSE;
	g_count = 0;
	g_capacity = 0;
	g_first = 0;
	g_last = 0;
	g_instructions_executed = 0;
	g_procs = NULL;
}

void _sal_proc_load_from(FILE *file)
{
	/* Load process module state from a binary file.
	*/
	assert(!g_is_init);
	assert(file);
	fread(&g_is_init, sizeof(boolean), 1, file);
	fread(&g_count, sizeof(uint32), 1, file);
	fread(&g_capacity, sizeof(uint32), 1, file);
	fread(&g_first, sizeof(uint32), 1, file);
	fread(&g_last, sizeof(uint32), 1, file);
	fread(&g_instructions_executed, sizeof(uint32), 1, file);
	g_procs = calloc(g_capacity, sizeof(Process));
	assert(g_procs);
	fread(g_procs, sizeof(Process), g_capacity, file);
}

void _sal_proc_save_into(FILE *file)
{
	/* Save process module state to a binary file.
	*/
	assert(g_is_init);
	assert(file);
	fwrite(&g_is_init, sizeof(boolean), 1, file);
	fwrite(&g_count, sizeof(uint32), 1, file);
	fwrite(&g_capacity, sizeof(uint32), 1, file);
	fwrite(&g_first, sizeof(uint32), 1, file);
	fwrite(&g_last, sizeof(uint32), 1, file);
	fwrite(&g_instructions_executed, sizeof(uint32), 1, file);
	fwrite(g_procs, sizeof(Process), g_capacity, file);
}

/* Getter methods for the process module.
*/
UINT32_GETTER(proc, count)
UINT32_GETTER(proc, capacity)
UINT32_GETTER(proc, first)
UINT32_GETTER(proc, last)
UINT32_GETTER(proc, instructions_executed)

boolean sal_proc_is_free(uint32 proc_id)
{
	/* In Salis, the reaper queue is implemented as a circular queue. Thus, at
	any given time, a process ID (which actually denotes a process 'address'
	or, more correctly, a process 'container address') might contain a living
	process or be empty. This function checks for the 'living' state of a given
	process ID.
	*/
	assert(g_is_init);
	assert(proc_id < g_capacity);

	if (!g_procs[proc_id].mb1s) {
		/* When running in debug mode, we make sure that non-living processes
		are completely set to zero, as this is the expected state.
		*/
		#ifndef NDEBUG
			Process dummy_proc;
			memset(&dummy_proc, 0, sizeof(Process));
			assert(!memcmp(&dummy_proc, &g_procs[proc_id], sizeof(Process)));
		#endif

		return TRUE;
	}

	return FALSE;
}

Process sal_proc_get_proc(uint32 proc_id)
{
	/* Get a **copy** (not a reference) of the process with the given ID. Note,
	this might be a non-living process.
	*/
	assert(g_is_init);
	assert(proc_id < g_capacity);
	return g_procs[proc_id];
}

void sal_proc_get_proc_data(uint32 proc_id, uint32_p buffer)
{
	/* Get a **copy** (not a reference) of the process with the given ID
	(represented as a string of 32 bit integers) written into the given buffer.
	The buffer must be pre-allocated to a large enough size
	(i.e. malloc(sizeof(Process))). Note, copied process might be in a
	non-living state.
	*/
	assert(g_is_init);
	assert(proc_id < g_capacity);
	assert(buffer);
	memcpy(buffer, &g_procs[proc_id], sizeof(Process));
}

static boolean block_is_free_and_valid(uint32 address, uint32 size)
{
	/* Iterate all addresses in the given memory block and check that they lie
	within memory bounds and have the ALLOCATED flag unset.
	*/
	uint32 offset;

	for (offset = 0; offset < size; offset++) {
		uint32 off_addr = offset + address;
		if (!sal_mem_is_address_valid(off_addr)) return FALSE;
		if (sal_mem_is_allocated(off_addr)) return FALSE;

		/* Deallocated addresses must have the BLOCK_START flag unset as well.
		*/
		assert(!sal_mem_is_block_start(off_addr));
	}

	return TRUE;
}

static void realloc_queue(uint32 queue_lock)
{
	/* Reallocate reaper queue into a new circular queue with double the
	capacity. This function gets called whenever the reaper queue fills up
	with new organisms.

	A queue_lock parameter may be provided, which 'centers' the reallocation on
	a given process ID. This means that, after reallocating the queue, the
	process with that ID will keep still have the same ID on the new queue.
	*/
	uint32 new_capacity;
	Process *new_queue;
	uint32 fwrd_idx;
	uint32 back_idx;
	assert(g_is_init);
	assert(g_count == g_capacity);
	assert(queue_lock < g_capacity);
	new_capacity = g_capacity * 2;
	new_queue = calloc(new_capacity, sizeof(Process));
	assert(new_queue);
	fwrd_idx = queue_lock;
	back_idx = (queue_lock - 1) % new_capacity;

	/* Copy all organisms that lie forward from queue lock.
	*/
	while (TRUE) {
		uint32 old_idx = fwrd_idx % g_capacity;
		memcpy(&new_queue[fwrd_idx], &g_procs[old_idx], sizeof(Process));

		if (old_idx == g_last) {
			g_last = fwrd_idx;
			break;
		} else {
			fwrd_idx++;
		}
	}

	/* Copy all organisms that lie backwards from queue lock, making sure to
	loop around the queue (with modulo '%') whenever the process index goes
	below zero.
	*/
	if (queue_lock != g_first) {
		while (TRUE) {
			uint32 old_idx = back_idx % g_capacity;
			memcpy(&new_queue[back_idx], &g_procs[old_idx], sizeof(Process));

			if (old_idx == g_first) {
				g_first = back_idx;
				break;
			} else {
				back_idx--;
				back_idx %= new_capacity;
			}
		}
	}

	/* Free old reaper queue and re-link global pointer to new queue.
	*/
	free(g_procs);
	g_capacity = new_capacity;
	g_procs = new_queue;
}

static uint32 get_new_proc_from_queue(uint32 queue_lock)
{
	/* Retrieve an unoccupied process ID from the reaper queue. This function
	gets called whenever a new organism is generated (born).
	*/
	assert(g_is_init);

	/* If reaper queue is full, reallocate to double its current size.
	*/
	if (g_count == g_capacity) {
		realloc_queue(queue_lock);
	}

	g_count++;

	if (g_count == 1) {
		g_first = 0;
		g_last = 0;
		return 0;
	} else {
		g_last++;
		g_last %= g_capacity;
		return g_last;
	}
}

static void proc_create(
	uint32 address, uint32 size, uint32 queue_lock,
	boolean set_ip, boolean allocate
) {
	/* Give birth to a new process! We must specify the address and size of the
	new organism.
	*/
	uint32 pidx;
	assert(g_is_init);
	assert(sal_mem_is_address_valid(address));
	assert(sal_mem_is_address_valid(address + size - 1));

	/* When organisms are generated manually (by an user), we must set the IP
	flag on the first byte of its owned memory. When organisms replicate by
	themselves, we don't set the flag, as it gets set at the end of the module
	cycle. Take a look at the '_sal_proc_cycle()' function for more info.
	*/
	if (set_ip) {
		_sal_mem_set_ip(address);
	}

	/* When organisms are generated manually (by an user), we must explicitly
	allocate its entire memory block. When organisms replicate by themselves,
	we assume they have already allocated the child's memory, so we don't need
	to do it here.
	*/
	if (allocate) {
		uint32 offset;
		assert(block_is_free_and_valid(address, size));
		_sal_mem_set_block_start(address);

		for (offset = 0; offset < size; offset++) {
			uint32 off_addr = offset + address;
			_sal_mem_set_allocated(off_addr);
		}
	}

	/* Get a new process ID for the child process. Also, set initial state of
	the child process data structure.
	*/
	pidx = get_new_proc_from_queue(queue_lock);
	g_procs[pidx].mb1a = address;
	g_procs[pidx].mb1s = size;
	g_procs[pidx].ip = address;
	g_procs[pidx].sp = address;
}

void sal_proc_create(uint32 address, uint32 mb1s)
{
	/* API function to create a new process. Memory address and size of new
	process must be provided.
	*/
	assert(g_is_init);
	assert(block_is_free_and_valid(address, mb1s));
	proc_create(address, mb1s, 0, TRUE, TRUE);
}

static void free_memory_block(uint32 address, uint32 size)
{
	/* Deallocate a memory block. This includes unsetting the BLOCK_START flag
	on the first byte.
	*/
	uint32 offset;
	assert(sal_mem_is_address_valid(address));
	assert(sal_mem_is_address_valid(address + size - 1));
	assert(sal_mem_is_block_start(address));
	assert(size);
	_sal_mem_unset_block_start(address);

	for (offset = 0; offset < size; offset++) {
		/* Iterate all addresses in block and unset the ALLOCATED flag in them.
		*/
		uint32 off_addr = offset + address;
		assert(sal_mem_is_allocated(off_addr));
		assert(!sal_mem_is_block_start(off_addr));
		_sal_mem_unset_allocated(off_addr);
	}
}

static void free_memory_owned_by(uint32 pidx)
{
	/* Free memory specifically owned by the process with the given ID.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	free_memory_block(g_procs[pidx].mb1a, g_procs[pidx].mb1s);

	if (g_procs[pidx].mb2s) {
		/* If process owns a child memory block, free it as well.
		*/
		free_memory_block(g_procs[pidx].mb2a, g_procs[pidx].mb2s);
	}
}

static void proc_kill(boolean reset_ips)
{
	/* Kill process on bottom of reaper queue (the oldest process).
	*/
	assert(g_is_init);
	assert(g_count);
	assert(g_first != UINT32_MAX);
	assert(g_last != UINT32_MAX);
	assert(!sal_proc_is_free(g_first));

	/* When called manually by an user, we must clear and reset the IP flags of
	all processes in order to preserve module validity.
	*/
	if (reset_ips) {
		_sal_mem_unset_ip(g_procs[g_first].ip);
	}

	/* Free up owned memory and reset process data structure back to zero.
	*/
	free_memory_owned_by(g_first);
	memset(&g_procs[g_first], 0, sizeof(Process));
	g_count--;

	if (g_first == g_last) {
		g_first = UINT32_MAX;
		g_last = UINT32_MAX;
	} else {
		g_first++;
		g_first %= g_capacity;
	}

	/* Reset IP flags of all living processes. We use openmp to do this faster.
	*/
	if (reset_ips) {
		uint32 pidx;

		#pragma omp parallel for
		for (pidx = 0; pidx < g_capacity; pidx++) {
			if (!sal_proc_is_free(pidx)) {
				_sal_mem_set_ip(g_procs[pidx].ip);
			}
		}
	}
}

void sal_proc_kill(void)
{
	/* API function to kill a process. Make sure that at least one process is
	alive, or 'assert()' will fail.
	*/
	assert(g_is_init);
	assert(g_count);
	assert(g_first != UINT32_MAX);
	assert(g_last != UINT32_MAX);
	assert(!sal_proc_is_free(g_first));
	proc_kill(TRUE);
}

static boolean block_is_allocated(uint32 address, uint32 size)
{
	/* Assert that a given memory block is fully allocated.
	*/
	uint32 offset;
	assert(g_is_init);

	for (offset = 0; offset < size; offset++) {
		uint32 off_addr = offset + address;
		assert(sal_mem_is_address_valid(off_addr));
		assert(sal_mem_is_allocated(off_addr));
	}

	return TRUE;
}

static boolean proc_is_valid(uint32 pidx)
{
	/* Assert that the process with the given ID is in a valid state. This
	means that all of its owned memory must be allocated and that the proper
	flags are set in place. IP and SP must be located in valid addresses.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);

	if (!sal_proc_is_free(pidx)) {
		assert(sal_mem_is_address_valid(g_procs[pidx].ip));
		assert(sal_mem_is_address_valid(g_procs[pidx].sp));
		assert(sal_mem_is_block_start(g_procs[pidx].mb1a));
		assert(sal_mem_is_ip(g_procs[pidx].ip));
		assert(block_is_allocated(g_procs[pidx].mb1a, g_procs[pidx].mb1s));

		if (g_procs[pidx].mb2s) {
			assert(sal_mem_is_block_start(g_procs[pidx].mb2a));
			assert(block_is_allocated(g_procs[pidx].mb2a, g_procs[pidx].mb2s));
		}
	}

	return TRUE;
}

static boolean module_is_valid(void)
{
	/* Check for validity of process module. This function only gets called
	when Salis is running in debug mode. It makes Salis **very** slow in
	comparison to when running optimized, but it is also **very** useful for
	debugging!
	*/
	uint32 pidx;
	uint32 alloc_count = 0;
	uint32 block_count = 0;
	assert(g_is_init);
	assert(g_count >= sal_mem_get_ip_count());

	/* Check that each individual process is in a valid state. We can do this
	in a multi-threaded way.
	*/
	#pragma omp parallel for
	for (pidx = 0; pidx < g_capacity; pidx++) {
		assert(proc_is_valid(pidx));
	}

	/* Iterate all processes, counting their memory blocks and adding up their
	memory block sizes. At the end, we compare the sums to the flag counters of
	the memory module.
	*/
	for (pidx = 0; pidx < g_capacity; pidx++) {
		if (!sal_proc_is_free(pidx)) {
			alloc_count += g_procs[pidx].mb1s;
			block_count++;

			if (g_procs[pidx].mb2s) {
				assert(g_procs[pidx].mb1a != g_procs[pidx].mb2a);
				alloc_count += g_procs[pidx].mb2s;
				block_count++;
			}
		}
	}

	assert(block_count == sal_mem_get_block_start_count());
	assert(alloc_count == sal_mem_get_allocated_count());
	return TRUE;
}

static void toggle_ip_flag(void (*toggler)(uint32 address))
{
	/* At the start of each process module cycle, all memory addresses with the
	IP flag set get their IP flag turned off. Once all processes finish
	executing, the IP flags are turned on again on all addresses pointed by
	'g_procs[pidx].ip'. I've found this is the easiest way to preserve
	correctness, given that more than one process can have their IPs pointed to
	the same address.

	This function simply iterates through all processes, setting the IP flag on
	or off on the address pointed to by their IP.
	*/
	uint32 pidx;
	assert(g_is_init);

	for (pidx = 0; pidx < g_capacity; pidx++) {
		if (!sal_proc_is_free(pidx)) {
			toggler(g_procs[pidx].ip);
		}
	}
}

static void on_fault(uint32 pidx)
{
	/* Organisms get punished whenever they execute an invalid instruction
	(commit a 'fault') by having the halt one simulation cycle.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	g_procs[pidx].punish = 1;
}

static void increment_ip(uint32 pidx)
{
	/* After executing each instruction, increment the given organism's IP to
	the next valid address.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (sal_mem_is_address_valid(g_procs[pidx].ip + 1)) {
		g_procs[pidx].ip++;
	}

	/* Wherever IP goes, SP follows. :P
	*/
	g_procs[pidx].sp = g_procs[pidx].ip;
}

static boolean are_templates_complements(uint32 source, uint32 complement)
{
	/* Check whether 2 templates are complements. Templates are introduced in
	Salis-2.0 and they function in the same way as templates in the original
	Tierra. They consist of string of NOP0 and NOP1 instructions.

	We say that templates are complements whenever one is a 'negation' of
	another (i.e. they are reverse copies of each other). So, on the following
	example, the top template would be the complement of the bottom template.

	>>> NOP0 - NOP1 - NOP1
	>>> NOP1 - NOP0 - NOP0

	This function looks into 2 given addresses in memory and checks whether
	there are complementing templates on those addresses.
	*/
	assert(g_is_init);
	assert(sal_mem_is_address_valid(source));
	assert(sal_mem_is_address_valid(complement));
	assert(sal_is_template(sal_mem_get_inst(source)));

	while (
		sal_mem_is_address_valid(source) &&
		sal_is_template(sal_mem_get_inst(source))
	) {
		/* Iterate address by address, checking complementarity on each
		consecutive byte pair.
		*/
		uint8 inst_src;
		uint8 inst_comp;

		/* If complement head moves to an invalid address, complementarity
		fails.
		*/
		if (!sal_mem_is_address_valid(complement)) {
			return FALSE;
		}

		inst_src = sal_mem_get_inst(source);
		inst_comp = sal_mem_get_inst(complement);
		assert(inst_src == NOP0 || inst_src == NOP1);

		if (inst_src == NOP0 && inst_comp != NOP1) {
			return FALSE;
		}

		if (inst_src == NOP1 && inst_comp != NOP0) {
			return FALSE;
		}

		source++;
		complement++;
	}

	/* If we get to the end of a template in the source head, and target has
	been complementary all the way through, we consider these blocks of memory
	'complements'.
	*/
	return TRUE;
}

static void increment_sp(uint32 pidx, boolean forward)
{
	/* Increment or decrement SP to the next valid address. This function gets
	called by organisms during jumps, searches, etc. (i.e. whenever the seeker
	pointer gets sent on a 'mission').
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (forward && sal_mem_is_address_valid(g_procs[pidx].sp + 1)) {
		g_procs[pidx].sp++;
	}

	if (!forward && sal_mem_is_address_valid(g_procs[pidx].sp - 1)) {
		g_procs[pidx].sp--;
	}
}

static boolean jump_seek(uint32 pidx, boolean forward)
{
	/* Search (via the seeker pointer) for template to jump into. This gets
	called by organisms each cycle during a JMP instruction. Only when a valid
	template is found, will this function return TRUE. Otherwise it will return
	FALSE, signaling the calling process that a template has not yet been
	found.
	*/
	uint32 next_addr;
	uint8 next_inst;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	next_addr = g_procs[pidx].ip + 1;

	/* This function causes a 'fault' when there is no template right in front
	of the caller organism's instruction pointer.
	*/
	if (!sal_mem_is_address_valid(next_addr)) {
		on_fault(pidx);
		increment_ip(pidx);
		return FALSE;
	}

	next_inst = sal_mem_get_inst(next_addr);

	if (!sal_is_template(next_inst)) {
		on_fault(pidx);
		increment_ip(pidx);
		return FALSE;
	}

	/* Check for complementarity. Increment seeker pointer if template has not
	been found yet.
	*/
	if (are_templates_complements(next_addr, g_procs[pidx].sp)) {
		return TRUE;
	}

	increment_sp(pidx, forward);
	return FALSE;
}

static void jump(uint32 pidx)
{
	/* This gets called when an organism has finally found a template to jump
	into (see function above). Only when in debug mode, we make sure that the
	entire jump operation has been performed in a valid way.
	*/
	#ifndef NDEBUG
		uint32 next_addr;
		uint8 next_inst;
		uint8 sp_inst;
		assert(g_is_init);
		assert(pidx < g_capacity);
		assert(!sal_proc_is_free(pidx));
		next_addr = g_procs[pidx].ip + 1;
		assert(sal_mem_is_address_valid(next_addr));
		next_inst = sal_mem_get_inst(next_addr);
		sp_inst = sal_mem_get_inst(g_procs[pidx].sp);
		assert(sal_is_template(next_inst));
		assert(sal_is_template(sp_inst));
		assert(are_templates_complements(next_addr, g_procs[pidx].sp));
	#endif

	g_procs[pidx].ip = g_procs[pidx].sp;
}

static boolean addr_seek(uint32 pidx, boolean forward)
{
	/* Search (via the seeker pointer) for template address in memory. This
	gets called by organisms each cycle during a ADR instruction. Only when a
	valid template is found, will this function return TRUE. Otherwise it will
	return FALSE, signaling the calling process that a template has not yet
	been found. */
	uint32 next1_addr;
	uint32 next2_addr;
	uint8 next1_inst;
	uint8 next2_inst;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	next1_addr = g_procs[pidx].ip + 1;
	next2_addr = g_procs[pidx].ip + 2;

	/* This function causes a 'fault' when there is no register modifier right
	in front of the caller organism's instruction pointer, and a template just
	after that.
	*/
	if (
		!sal_mem_is_address_valid(next1_addr) ||
		!sal_mem_is_address_valid(next2_addr)
	) {
		on_fault(pidx);
		increment_ip(pidx);
		return FALSE;
	}

	next1_inst = sal_mem_get_inst(next1_addr);
	next2_inst = sal_mem_get_inst(next2_addr);

	if (
		!sal_is_mod(next1_inst) ||
		!sal_is_template(next2_inst)
	) {
		on_fault(pidx);
		increment_ip(pidx);
		return FALSE;
	}

	/* Check for complementarity. Increment seeker pointer if template has not
	been found yet.
	*/
	if (are_templates_complements(next2_addr, g_procs[pidx].sp)) {
		return TRUE;
	}

	increment_sp(pidx, forward);
	return FALSE;
}

static boolean get_register_pointers(
	uint32 pidx, uint32_p *regs, uint32 reg_count
) {
	/* This function is used to get pointers to a calling organism registers.
	Specifically, registers returned are those that will be used when executing
	the caller organism's current instruction.
	*/
	uint32 ridx;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	assert(regs);
	assert(reg_count);
	assert(reg_count < 4);

	/* Iterate 'reg_count' number of instructions forward from the IP, noting
	down all found register modifiers. If less than 'reg_count' modifiers are
	found, this function returns FALSE (triggering a 'fault').
	*/
	for (ridx = 0; ridx < reg_count; ridx++) {
		uint32 mod_addr = g_procs[pidx].ip + 1 + ridx;

		if (
			!sal_mem_is_address_valid(mod_addr) ||
			!sal_is_mod(sal_mem_get_inst(mod_addr))
		) {
			return FALSE;
		}

		switch (sal_mem_get_inst(mod_addr)) {
		case MODA:
			regs[ridx] = &g_procs[pidx].rax;
			break;
		case MODB:
			regs[ridx] = &g_procs[pidx].rbx;
			break;
		case MODC:
			regs[ridx] = &g_procs[pidx].rcx;
			break;
		case MODD:
			regs[ridx] = &g_procs[pidx].rdx;
			break;
		}
	}

	return TRUE;
}

static void addr(uint32 pidx)
{
	/* This gets called when an organism has finally found a template and is
	ready to store its address. Only when in debug mode, we make sure that the
	entire search operation has been performed in a valid way.
	*/
	uint32_p reg;

	#ifndef NDEBUG
		uint32 next2_addr;
		uint8 next2_inst;
		uint8 sp_inst;
		assert(g_is_init);
		assert(pidx < g_capacity);
		assert(!sal_proc_is_free(pidx));
		next2_addr = g_procs[pidx].ip + 2;
		assert(sal_mem_is_address_valid(next2_addr));
		next2_inst = sal_mem_get_inst(next2_addr);
		sp_inst = sal_mem_get_inst(g_procs[pidx].sp);
		assert(sal_is_template(next2_inst));
		assert(sal_is_template(sp_inst));
		assert(are_templates_complements(next2_addr, g_procs[pidx].sp));
	#endif

	/* Store address of complement into the given register.
	*/
	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	*reg = g_procs[pidx].sp;
	increment_ip(pidx);
}

static void free_child_block_of(uint32 pidx)
{
	/* Free only the 'child' memory block (mb2) of the caller organism.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	assert(g_procs[pidx].mb2s);
	free_memory_block(g_procs[pidx].mb2a, g_procs[pidx].mb2s);
	g_procs[pidx].mb2a = 0;
	g_procs[pidx].mb2s = 0;
}

static void alloc(uint32 pidx, boolean forward)
{
	/* Allocate a 'child' memory block of size stored in the first given
	register, and save its address into the second given register. This
	function is the basis of Salisian reproduction. It's a fairly complicated
	function (as the seeker pointer must function in a procedural way), so it's
	divided into a series of steps, documented below.
	*/
	uint32_p regs[2];
	uint32 block_size;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	/* For this function to work, we need at least two register modifiers.
	Then, we check for all possible error conditions. If any error conditions
	are found, the instruction faults and returns.
	*/
	if (!get_register_pointers(pidx, regs, 2)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	block_size = *regs[0];

	/* ERROR 1: requested child block is of size zero.
	*/
	if (!block_size) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	/* ERROR 2: seeker pointer not adjacent to existing child block.
	*/
	if (g_procs[pidx].mb2s) {
		uint32 exp_addr;

		if (forward) {
			exp_addr = g_procs[pidx].mb2a + g_procs[pidx].mb2s;
		} else {
			exp_addr = g_procs[pidx].mb2a - 1;
		}

		if (g_procs[pidx].sp != exp_addr) {
			on_fault(pidx);
			increment_ip(pidx);
			return;
		}
	}

	/* No errors were detected. We thus handle all correct conditions.
	* CONDITION 1: allocation was successful.
	*/
	if (g_procs[pidx].mb2s == block_size) {
		increment_ip(pidx);
		*regs[1] = g_procs[pidx].mb2a;
		return;
	}

	/* CONDITION 2: seeker pointer has collided with allocated space. We free
	child memory block and just continue searching.
	*/
	if (sal_mem_is_allocated(g_procs[pidx].sp)) {
		if (g_procs[pidx].mb2s) {
			free_child_block_of(pidx);
		}

		increment_sp(pidx, forward);
		return;
	}

	/* CONDITION 3: no collision detected; enlarge child memory block and
	increment seeker pointer. Also, correct position of BLOCK_START bit flag.
	*/
	_sal_mem_set_allocated(g_procs[pidx].sp);

	if (!g_procs[pidx].mb2s) {
		g_procs[pidx].mb2a = g_procs[pidx].sp;
		_sal_mem_set_block_start(g_procs[pidx].sp);
	} else if (!forward) {
		_sal_mem_unset_block_start(g_procs[pidx].mb2a);
		g_procs[pidx].mb2a = g_procs[pidx].sp;
		_sal_mem_set_block_start(g_procs[pidx].mb2a);
	}

	g_procs[pidx].mb2s++;
	increment_sp(pidx, forward);
}

static void swap(uint32 pidx)
{
	/* Swap parent and child memory blocks. This function is the basis of
	Salisian metabolism.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (g_procs[pidx].mb2s) {
		uint32 addr_temp = g_procs[pidx].mb1a;
		uint32 size_temp = g_procs[pidx].mb1s;
		g_procs[pidx].mb1a = g_procs[pidx].mb2a;
		g_procs[pidx].mb1s = g_procs[pidx].mb2s;
		g_procs[pidx].mb2a = addr_temp;
		g_procs[pidx].mb2s = size_temp;
	} else {
		on_fault(pidx);
	}

	increment_ip(pidx);
}

static void split(uint32 pidx)
{
	/* Split child memory block into a new organism. A new baby is born. :-)
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (g_procs[pidx].mb2s) {
		proc_create(
			g_procs[pidx].mb2a, g_procs[pidx].mb2s, pidx, FALSE, FALSE
		);
		g_procs[pidx].mb2a = 0;
		g_procs[pidx].mb2s = 0;
	} else {
		on_fault(pidx);
	}

	increment_ip(pidx);
}

static void one_reg_op(uint32 pidx, uint8 inst)
{
	/* Here we group all 1-register operations. These include incrementing,
	decrementing, placing zero or one on a register, and the negation
	operation.
	*/
	uint32_p reg;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	assert(sal_is_inst(inst));

	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	switch (inst) {
	case INCN:
		(*reg)++;
		break;
	case DECN:
		(*reg)--;
		break;
	case ZERO:
		(*reg) = 0;
		break;
	case UNIT:
		(*reg) = 1;
		break;
	case NOTN:
		(*reg) = !(*reg);
		break;
	default:
		assert(FALSE);
	}

	increment_ip(pidx);
}

static void if_not_zero(uint32 pidx)
{
	/* Conditional operator. Like in most programming languages, this
	instruction is needed to allow organism execution to branch into different
	execution streams.
	*/
	uint32_p reg;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	if (!(*reg)) {
		increment_ip(pidx);
	}

	increment_ip(pidx);
	increment_ip(pidx);
}

static void three_reg_op(uint32 pidx, uint8 inst)
{
	/* Here we group all 3-register arithmetic operations. These include
	addition, subtraction, multiplication and division.
	*/
	uint32_p regs[3];
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	assert(sal_is_inst(inst));

	if (!get_register_pointers(pidx, regs, 3)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	switch (inst) {
	case SUMN:
		*regs[0] = *regs[1] + *regs[2];
		break;
	case SUBN:
		*regs[0] = *regs[1] - *regs[2];
		break;
	case MULN:
		*regs[0] = *regs[1] * *regs[2];
		break;
	case DIVN:
		/* Division by 0 is not allowed and causes a fault. */
		if (!(*regs[2])) {
			on_fault(pidx);
			increment_ip(pidx);
			return;
		}

		*regs[0] = *regs[1] / *regs[2];
		break;
	default:
		assert(FALSE);
	}

	increment_ip(pidx);
}

static void load(uint32 pidx)
{
	/* Load an instruction from a given address into a specified register. This
	is used by organisms during their reproduction cycle.
	*/
	uint32_p regs[2];
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (
		!get_register_pointers(pidx, regs, 2) ||
		!sal_mem_is_address_valid(*regs[0])
	) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	if (g_procs[pidx].sp < *regs[0]) {
		increment_sp(pidx, TRUE);
	} else if (g_procs[pidx].sp > *regs[0]) {
		increment_sp(pidx, FALSE);
	} else {
		*regs[1] = sal_mem_get_inst(*regs[0]);
		increment_ip(pidx);
	}
}

static boolean is_writeable_by(uint32 pidx, uint32 address)
{
	/* Check whether an organisms has writing rights on a specified address.
	Any organism may write to any valid address that is either self owned or
	not allocated.
	*/
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	assert(sal_mem_is_address_valid(address));

	if (!sal_mem_is_allocated(address)) {
		return TRUE;
	} else {
		uint32 lo1 = g_procs[pidx].mb1a;
		uint32 lo2 = g_procs[pidx].mb2a;
		uint32 hi1 = lo1 + g_procs[pidx].mb1s;
		uint32 hi2 = lo2 + g_procs[pidx].mb2s;
		return (
			(address >= lo1 && address < hi1) ||
			(address >= lo2 && address < hi2)
		);
	}
}

static void write(uint32 pidx)
{
	/* Write instruction on a given register into a specified address. This is
	used by organisms during their reproduction cycle.
	*/
	uint32_p regs[2];
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (
		!get_register_pointers(pidx, regs, 2) ||
		!sal_mem_is_address_valid(*regs[0]) ||
		!sal_is_inst(*regs[1])
	) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	if (g_procs[pidx].sp < *regs[0]) {
		increment_sp(pidx, TRUE);
	} else if (g_procs[pidx].sp > *regs[0]) {
		increment_sp(pidx, FALSE);
	} else if (is_writeable_by(pidx, *regs[0])) {
		sal_mem_set_inst(*regs[0], *regs[1]);
		increment_ip(pidx);
	} else {
		on_fault(pidx);
		increment_ip(pidx);
	}
}

static void send(uint32 pidx)
{
	/* Send instruction on given register into the common pipe.
	*/
	uint32_p reg;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	if (!sal_is_inst(*reg)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	_sal_comm_send((uint8)(*reg));
	increment_ip(pidx);
}

static void receive(uint32 pidx)
{
	/* Receive a single instruction from the common pipe and store it into a
	specified register. In case the common pipe is empty, it will return the
	NOP0 instruction.
	*/
	uint32_p reg;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	*reg = _sal_comm_receive();
	assert(sal_is_inst(*reg));
	increment_ip(pidx);
}

static void push(uint32 pidx)
{
	/* Push value on register into the stack. This is useful as a secondary
	memory resource.
	*/
	uint32_p reg;
	uint32 sidx;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	for (sidx = 7; sidx; sidx--) {
		g_procs[pidx].stack[sidx] = g_procs[pidx].stack[sidx - 1];
	}

	g_procs[pidx].stack[0] = *reg;
	increment_ip(pidx);
}

static void
pop(uint32 pidx)
{
	/* Pop value from the stack into a given register.
	*/
	uint32_p reg;
	uint32 sidx;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	if (!get_register_pointers(pidx, &reg, 1)) {
		on_fault(pidx);
		increment_ip(pidx);
		return;
	}

	*reg = g_procs[pidx].stack[0];

	for (sidx = 1; sidx < 8; sidx++) {
		g_procs[pidx].stack[sidx - 1] = g_procs[pidx].stack[sidx];
	}

	g_procs[pidx].stack[7] = 0;
	increment_ip(pidx);
}

static boolean eat_seek(uint32 pidx, boolean forward)
{
	/* Search (via the seeker pointer) for an identical copy of the memory
	stream right in front of the calling organism's IP. This function gets
	called by organisms each cycle during an EAT instruction. Only when a valid
	copy is found, this function will return TRUE. */
	uint32 next_addr;
	uint8 next_inst;
	uint8 sp_inst;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	next_addr = g_procs[pidx].ip + 1;

	if (!sal_mem_is_address_valid(next_addr)) {
		on_fault(pidx);
		increment_ip(pidx);
		return FALSE;
	}

	if (g_procs[pidx].sp == next_addr) {
		increment_sp(pidx, forward);
		return FALSE;
	}

	next_inst = sal_mem_get_inst(next_addr);
	sp_inst = sal_mem_get_inst(g_procs[pidx].sp);

	if (next_inst == sp_inst) {
		return TRUE;
	}

	increment_sp(pidx, forward);
	return FALSE;
}

static void eat(uint32 pidx)
{
	/* Salisian organisms may 'eat' information. They eat by searching for
	'copies' of the code in front of their IPs during the EAT instruction. When
	a valid copy is found, an organism gets rewarded by setting their 'reward'
	field to the length of the measured copy. Each cycle, organisms execute
	'reward' number of instructions plus one, thus, eating a larger stream
	produces a larger advantage for an organism.

	However, whenever an organism eats, the detected copy of the source code
	gets destroyed (randomized). The main idea of the EAT instruction is to
	turn 'information' into a valuable resource in Salis.
	*/
	uint32 source;
	uint32 target;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));
	source = g_procs[pidx].ip + 1;
	target = g_procs[pidx].sp;
	assert(sal_mem_is_address_valid(source));
	assert(sal_mem_get_inst(source) == sal_mem_get_inst(target));
	g_procs[pidx].reward = 0;

	while (
		sal_mem_is_address_valid(source) &&
		sal_mem_is_address_valid(target) &&
		sal_mem_get_inst(source) == sal_mem_get_inst(target)
	) {
		g_procs[pidx].reward++;
		_sal_evo_randomize_at(target);
		source++;
		target++;
	}

	increment_ip(pidx);
}

static void proc_cycle(uint32 pidx)
{
	/* Cycle a process once. During each process cycle, several things may
	happen. For example, if a process is being punished (for committing a
	fault), it will have to wait until the next simulation cycle to be able to
	execute.

	Non-punished organisms execute at least one instruction per simulation
	cycle. If they are being rewarded, they execute one, plus the number on
	their 'reward' field, number of instructions each cycle.
	*/
	uint32 cycles;
	assert(g_is_init);
	assert(pidx < g_capacity);
	assert(!sal_proc_is_free(pidx));

	/* Organism is being punished. Clear its 'punish' field and return without
	executing.
	*/
	if (g_procs[pidx].punish) {
		g_procs[pidx].punish = 0;
		return;
	}

	/* Execute one instruction per number of 'reward' points awarded to this
	organism. Switch case associates each instruction to its corresponding
	instruction handler. Process module keeps track of the total number of
	instructions executed (by all organisms) per simulation cycle.
	*/
	for (cycles = 0; cycles < g_procs[pidx].reward + 1; cycles++) {
		uint8 inst = sal_mem_get_inst(g_procs[pidx].ip);
		g_instructions_executed++;

		switch (inst) {
		case JMPB:
			if (jump_seek(pidx, FALSE)) jump(pidx);
			break;
		case JMPF:
			if (jump_seek(pidx, TRUE)) jump(pidx);
			break;
		case ADRB:
			if (addr_seek(pidx, FALSE)) addr(pidx);
			break;
		case ADRF:
			if (addr_seek(pidx, TRUE)) addr(pidx);
			break;
		case MALB:
			alloc(pidx, FALSE);
			break;
		case MALF:
			alloc(pidx, TRUE);
			break;
		case SWAP:
			swap(pidx);
			break;
		case SPLT:
			split(pidx);
			break;
		case INCN:
		case DECN:
		case ZERO:
		case UNIT:
		case NOTN:
			one_reg_op(pidx, inst);
			break;
		case IFNZ:
			if_not_zero(pidx);
			break;
		case SUMN:
		case SUBN:
		case MULN:
		case DIVN:
			three_reg_op(pidx, inst);
			break;
		case LOAD:
			load(pidx);
			break;
		case WRTE:
			write(pidx);
			break;
		case SEND:
			send(pidx);
			break;
		case RECV:
			receive(pidx);
			break;
		case PSHN:
			push(pidx);
			break;
		case POPN:
			pop(pidx);
			break;
		case EATB:
			if (eat_seek(pidx, FALSE)) eat(pidx);
			break;
		case EATF:
			if (eat_seek(pidx, TRUE)) eat(pidx);
			break;
		default:
			increment_ip(pidx);
		}
	}
}

void _sal_proc_cycle(void)
{
	/* The process module cycle consists of a series of steps, which are needed
	to preserve overall correctness.
	*/
	assert(g_is_init);
	assert(module_is_valid());
	g_instructions_executed = 0;

	/* Iterate through all organisms in the reaper queue. First organism to
	execute is the one pointed to by 'g_last' (the one on top of the queue).
	Last one to execute is 'g_first'. We go around the circular queue, making
	sure to modulo (%) around when iterator goes below zero.
	*/
	if (g_count) {
		uint32 pidx = g_last;

		/* Turn off all IP flags in memory and cycle 'g_last'. Then, continue
		with all other organisms until we reach 'g_first'.
		*/
		toggle_ip_flag(_sal_mem_unset_ip);
		assert(!sal_mem_get_ip_count());
		proc_cycle(pidx);

		while (pidx != g_first) {
			pidx--;
			pidx %= g_capacity;
			proc_cycle(pidx);
		}

		/* Kill oldest processes whenever memory gets filled over capacity.
		*/
		while (sal_mem_get_allocated_count() > sal_mem_get_capacity()) {
			proc_kill(FALSE);
		}

		/* Finally, turn IP flags back on. Keep in mind that IP flags exist
		for visualization purposes only. They are actually not really needed.
		*/
		toggle_ip_flag(_sal_mem_set_ip);
	}
}