/
VmOperations.cs
352 lines (277 loc) · 13.8 KB
/
VmOperations.cs
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
namespace MetaNix.search.levin2 {
// wrapper for two parameter call with success
static class ArrayOperationsTwoArgumentWrapper {
public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta, int dummy, out bool success) {
ArrayOperations.arrayMove(globalState, localState, delta);
success = true;
}
public static void arrayRemove(GlobalInterpreterState globalState, LocalInterpreterState localState, int dummy0, int dummy1, out bool success) {
ArrayOperations.arrayRemove(globalState, localState, out success);
}
public static void arrayCompareWithRegister(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, int dummy0, out bool success) {
ArrayOperations.arrayCompareWithRegister(globalState, localState, register, out success);
}
}
// wrappr for two parameters call with cuccess
static class OperationsTwoArgumentWrapper {
public static void jumpIfNotFlag(LocalInterpreterState state, int delta, int dummy0, out bool success) {
Operations.jumpIfNotFlag(state, delta);
success = true;
}
public static void jumpIfFlag(LocalInterpreterState state, int delta, int dummy0, out bool success) {
Operations.jumpIfFlag(state, delta);
success = true;
}
public static void jump(LocalInterpreterState state, int delta, int dummy0, out bool success) {
Operations.jump(state, delta);
success = true;
}
}
public static class Operations {
public static void @return(LocalInterpreterState state, out bool success) {
state.instructionPointer = state.callstack.pop(out success);
}
public static void jumpIfNotFlag(LocalInterpreterState state, int delta) {
if (!state.comparisionFlag) {
state.instructionPointer += delta;
}
state.instructionPointer++;
}
public static void jumpIfFlag(LocalInterpreterState state, int delta) {
if (state.comparisionFlag) {
state.instructionPointer += delta;
}
state.instructionPointer++;
}
public static void jump(LocalInterpreterState state, int delta) {
state.instructionPointer += delta;
state.instructionPointer++;
}
public static void movImmediate(LocalInterpreterState state, int register, int value, out bool success) {
state.registers[register] = value;
state.instructionPointer++;
success = true;
}
public static void call(LocalInterpreterState state, int delta) {
state.callstack.push(state.instructionPointer + 1);
state.instructionPointer += delta;
state.instructionPointer++;
}
// /param type 0 : equality, -1 less than, 1 greater than
public static void compareImmediate(LocalInterpreterState state, int register, int value, int type) {
if( type == 0 ) state.comparisionFlag = state.registers[register] == value;
else if (type == -1) state.comparisionFlag = state.registers[register] < value;
else state.comparisionFlag = state.registers[register] > value;
state.instructionPointer++;
}
// /param type 0 : equality, -1 less than, 1 greater than
public static void compareRegister(LocalInterpreterState state, int registerRight, int registerLeft, int type) {
if (type == 0) state.comparisionFlag = state.registers[registerRight] == state.registers[registerLeft];
else if (type == -1) state.comparisionFlag = state.registers[registerRight] < state.registers[registerLeft];
else state.comparisionFlag = state.registers[registerRight] > state.registers[registerLeft];
state.instructionPointer++;
}
public static void add(LocalInterpreterState state, int register, int value) {
state.registers[register] += value;
state.instructionPointer++;
}
public static void mulRegisterImmediate(LocalInterpreterState state, int register, int value) {
state.registers[register] *= value;
state.instructionPointer++;
}
public static void mulRegisterRegister(LocalInterpreterState state, int registerDestination, int registerSource) {
state.registers[registerDestination] *= state.registers[registerSource];
state.instructionPointer++;
}
public static void addRegisterRegister(LocalInterpreterState state, int registerDestination, int registerSource) {
state.registers[registerDestination] += state.registers[registerSource];
state.instructionPointer++;
}
public static void subRegisterRegister(LocalInterpreterState state, int registerDestination, int registerSource) {
state.registers[registerDestination] -= state.registers[registerSource];
state.instructionPointer++;
}
// add by checking flag
public static void addFlag(LocalInterpreterState state, int register, int value) {
if (state.comparisionFlag) {
state.registers[register] += value;
}
state.instructionPointer++;
}
// interprets the value as binary (zero is false and all else is true) and negates it
public static void binaryNegate(GlobalInterpreterState globalState, LocalInterpreterState localState, int register) {
localState.registers[register] = (localState.registers[register] == 0) ? 1 : 0;
localState.instructionPointer++;
}
// random number up to the value of the register
public static void random(GlobalInterpreterState globalState, LocalInterpreterState localState, int destRegister, int register, out bool success) {
if (localState.registers[register] <= 0) {
success = false;
return;
}
localState.registers[destRegister] = globalState.rng.Next(localState.registers[register]);
success = true;
}
}
public static class ArrayOperations {
public static void arrayMove(GlobalInterpreterState globalState, LocalInterpreterState localState, int delta) {
globalState.arrayState.index += delta;
localState.instructionPointer++;
}
public static void arrayRemove(GlobalInterpreterState globalState, LocalInterpreterState localState, out bool success) {
if (globalState.arrayState == null || !globalState.arrayState.isIndexValid) {
success = false;
return;
}
globalState.arrayState.array.removeAt(globalState.arrayState.index);
localState.instructionPointer++;
success = true;
}
public static void arrayCompareWithRegister(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, out bool success) {
if (globalState.arrayState == null || !globalState.arrayState.isIndexValid) {
success = false;
return;
}
localState.comparisionFlag = localState.registers[register] == globalState.arrayState.array[globalState.arrayState.index];
localState.instructionPointer++;
success = true;
}
public static void insert(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
if (globalState.arrayState.index < 0 || globalState.arrayState.index > globalState.arrayState.array.count) {
success = false;
return;
}
int valueToInsert = localState.registers[register];
globalState.arrayState.array.insert(globalState.arrayState.index, valueToInsert);
localState.instructionPointer++;
success = true;
}
// array is ignored
public static void setIdxRelative(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int index, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
if (index == -1) { // end of array, so insertion appends an element
globalState.arrayState.index = globalState.arrayState.array.count;
}
else {
globalState.arrayState.index = index;
}
localState.instructionPointer++;
success = true;
}
// moves the array index by delta and stores in the flag if the index is still in bound after moving
public static void idxFlag(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int delta, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
if (array == 0) {
globalState.arrayState.index += delta;
localState.comparisionFlag = globalState.arrayState.isIndexValid; // store the validity of the arrayIndex in flag
}
else {
success = false;
return;
}
localState.instructionPointer++;
success = true;
}
// /param array is the index of the array (currently ignored)
public static void valid(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
localState.comparisionFlag = globalState.arrayState.isIndexValid;
localState.instructionPointer++;
success = true;
}
public static void read(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int register, out bool success) {
if (globalState.arrayState == null || !globalState.arrayState.isIndexValid) {
success = false;
return;
}
localState.registers[register] = globalState.arrayState.array[globalState.arrayState.index];
localState.instructionPointer++;
success = true;
}
public static void idx2Reg(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int register, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
localState.registers[register] = globalState.arrayState.index;
localState.instructionPointer++;
success = true;
}
public static void reg2idx(GlobalInterpreterState globalState, LocalInterpreterState localState, int register, int array, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
globalState.arrayState.index = localState.registers[register];
localState.instructionPointer++;
success = true;
}
public static void arrayMovToArray(GlobalInterpreterState globalState, LocalInterpreterState localState, int array, int register, out bool success) {
if (globalState.arrayState == null || !globalState.arrayState.isIndexValid) {
success = false;
return;
}
globalState.arrayState.array[globalState.arrayState.index] = localState.registers[register];
localState.instructionPointer++;
success = true;
}
// macro-arrAdvanceOrExit
// advance array index and reset and return/terminate if its over
// else jump relative
public static void macroArrayAdvanceOrExit(GlobalInterpreterState globalState, LocalInterpreterState localState, int ipDelta, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
globalState.arrayState.index++;
bool isIndexValid = globalState.arrayState.isIndexValid;
if (!isIndexValid) {
globalState.arrayState.index = 0;
Operations.@return(localState, out success);
return;
}
localState.instructionPointer++;
localState.instructionPointer += ipDelta;
success = true;
}
// macro-arrNotEndOrExit
// advance array index and reset and return/terminate if its over
// else jump relative
public static void macroArrayNotEndOrExit(GlobalInterpreterState globalState, LocalInterpreterState localState, int ipDelta, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
bool isIndexValid = globalState.arrayState.isIndexValid;
if (!isIndexValid) {
globalState.arrayState.index = 0;
Operations.@return(localState, out success);
return;
}
localState.instructionPointer++;
localState.instructionPointer += ipDelta;
success = true;
}
public static void length(GlobalInterpreterState globalState, LocalInterpreterState localState, int destRegister, out bool success) {
if (globalState.arrayState == null) {
success = false;
return;
}
localState.registers[destRegister] = globalState.arrayState.array.count;
success = true;
}
}
}