-
Notifications
You must be signed in to change notification settings - Fork 0
/
is.go
477 lines (403 loc) · 8.35 KB
/
is.go
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
package soda
import (
"fmt"
"math"
)
func sodaIS(ins byte) Operation {
switch ins {
case 0x00:
return halt
case 0x01:
return breakpoint
case 0x02:
return move
case 0x0A:
return nRegSet
case 0x0B:
return pRegSet
case 0x0C:
return cReg
case 0x10:
return zero
case 0x11:
return addition
case 0x12:
return subtraction
case 0x13:
return multiplication
case 0x14:
return division
case 0x15:
return mod
case 0x16:
return power
case 0x19:
return uaddition
case 0x1A:
return usubtraction
case 0x1B:
return umultiplication
case 0x1C:
return udivision
case 0x1D:
return umod
case 0x1E:
return upower
case 0x21:
return additionI
case 0x22:
return subtractionI
case 0x23:
return multiplicationI
case 0x24:
return divisionI
case 0x25:
return modI
case 0x26:
return powerI
case 0x29:
return uadditionI
case 0x2A:
return usubtractionI
case 0x2B:
return umultiplicationI
case 0x2C:
return udivisionI
case 0x2D:
return umodI
case 0x2E:
return upowerI
case 0x31:
return and
case 0x32:
return or
case 0x33:
return xor
case 0x34:
return invert
case 0x35:
return lshift
case 0x36:
return rshift
case 0x50:
return malloc
case 0x51:
return load
case 0x52:
return loadb
case 0x53:
return loadi
case 0x54:
return loadil
case 0x55:
return loadih
case 0x56:
return store
case 0x57:
return storeb
case 0x5F:
return free
case 0x61:
return jump
case 0x62:
return jumpEq
case 0x63:
return jumpNeq
case 0x64:
return jumpLess
case 0x65:
return jumpLessEq
case 0x68:
return branch
case 0x69:
return branchEq
case 0x6A:
return branchNeq
case 0x6B:
return branchLess
case 0x6C:
return branchLessEq
case 0x81:
return printi
case 0x82:
return printc
case 0x89:
return printii
case 0x8a:
return printci
}
return undefined
}
func undefined(v *vm, a, b, c byte) error {
panic(UndefinedBehaviour)
}
func halt(v *vm, a, b, c byte) error {
v.halting = true
return nil
}
func nRegSet(v *vm, a, b, c byte) error {
v.currset++
v.regs = &v.regsets[v.currset]
return nil
}
func pRegSet(v *vm, a, b, c byte) error {
v.currset--
v.regs = &v.regsets[v.currset]
return nil
}
func cReg(v *vm, a, b, c byte) error {
v.regsets[v.regs[a]][v.regs[c]] = v.regsets[v.regs[b]][v.regs[c]]
return nil
}
func move(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b]
return nil
}
func breakpoint(v *vm, a, b, c byte) error {
v.Break()
return nil
}
func zero(v *vm, a, b, c byte) error {
v.regs[a] = 0
return nil
}
func addition(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] + v.regs[c]
return nil
}
func subtraction(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] - v.regs[c]
return nil
}
func multiplication(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] * v.regs[c]
return nil
}
func division(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] / v.regs[c]
return nil
}
func mod(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] % v.regs[c]
return nil
}
func power(v *vm, a, b, c byte) error {
v.regs[a] = register(math.Pow(float64(v.regs[b]), float64(v.regs[c])))
return nil
}
func uaddition(v *vm, a, b, c byte) error {
v.regs[a] = register(uint32(v.regs[b]) + uint32(v.regs[c]))
return nil
}
func usubtraction(v *vm, a, b, c byte) error {
v.regs[a] = register(uint32(v.regs[b]) - uint32(v.regs[c]))
return nil
}
func umultiplication(v *vm, a, b, c byte) error {
v.regs[a] = register(uint32(v.regs[b]) * uint32(v.regs[c]))
return nil
}
func udivision(v *vm, a, b, c byte) error {
v.regs[a] = register(uint32(v.regs[b]) / uint32(v.regs[c]))
return nil
}
func umod(v *vm, a, b, c byte) error {
v.regs[a] = register(uint32(v.regs[b]) % uint32(v.regs[c]))
return nil
}
func upower(v *vm, a, b, c byte) error {
v.regs[a] = register(math.Pow(float64(uint32(v.regs[b])), float64(uint32(v.regs[c]))))
return nil
}
/*
Immediate artithmetic operations
*/
func additionI(v *vm, a, b, c byte) error {
v.regs[a] += register(b)<<16 | register(c)
return nil
}
func subtractionI(v *vm, a, b, c byte) error {
v.regs[a] -= register(b)<<16 | register(c)
return nil
}
func multiplicationI(v *vm, a, b, c byte) error {
v.regs[a] *= register(b)<<16 | register(c)
return nil
}
func divisionI(v *vm, a, b, c byte) error {
v.regs[a] /= register(b)<<16 | register(c)
return nil
}
func modI(v *vm, a, b, c byte) error {
v.regs[a] %= register(b)<<16 | register(c)
return nil
}
func powerI(v *vm, a, b, c byte) error {
v.regs[a] = register(math.Pow(float64(v.regs[a]), float64(register(b)<<16|register(c))))
return nil
}
func uadditionI(v *vm, a, b, c byte) error {
v.regs[a] += register(uint32(b)<<16 | uint32(c))
return nil
}
func usubtractionI(v *vm, a, b, c byte) error {
v.regs[a] -= register(uint32(b)<<16 | uint32(c))
return nil
}
func umultiplicationI(v *vm, a, b, c byte) error {
v.regs[a] *= register(uint32(b)<<16 | uint32(c))
return nil
}
func udivisionI(v *vm, a, b, c byte) error {
v.regs[a] /= register(uint32(b)<<16 | uint32(c))
return nil
}
func umodI(v *vm, a, b, c byte) error {
v.regs[a] %= register(uint32(b)<<16 | uint32(c))
return nil
}
func upowerI(v *vm, a, b, c byte) error {
v.regs[a] = register(math.Pow(
float64(uint32(v.regs[a])),
float64(register(b)<<16|register(c))))
return nil
}
/*
Logical operations
*/
func and(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] & v.regs[c]
return nil
}
func or(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] | v.regs[c]
return nil
}
func xor(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] ^ v.regs[c]
return nil
}
func invert(v *vm, a, b, c byte) error {
v.regs[a] = ^v.regs[b]
return nil
}
func lshift(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] << uint32(v.regs[c])
return nil
}
func rshift(v *vm, a, b, c byte) error {
v.regs[a] = v.regs[b] >> uint32(v.regs[c])
return nil
}
func jump(v *vm, a, b, c byte) error {
v.code.Seek(int64(v.regs[a]), 0)
return nil
}
func jumpEq(v *vm, a, b, c byte) error {
if v.regs[b] == v.regs[c] {
v.code.Seek(int64(v.regs[a]), 0)
}
return nil
}
func jumpNeq(v *vm, a, b, c byte) error {
if v.regs[b] != v.regs[c] {
v.code.Seek(int64(v.regs[a]), 0)
}
return nil
}
func jumpLess(v *vm, a, b, c byte) error {
if v.regs[b] < v.regs[c] {
v.code.Seek(int64(v.regs[a]), 0)
}
return nil
}
func jumpLessEq(v *vm, a, b, c byte) error {
if v.regs[b] <= v.regs[c] {
v.code.Seek(int64(v.regs[a]), 0)
}
return nil
}
func branch(v *vm, a, b, c byte) error {
v.code.Seek(int64(v.regs[a]), 1)
return nil
}
func branchEq(v *vm, a, b, c byte) error {
if v.regs[b] == v.regs[c] {
v.code.Seek(int64(v.regs[a]), 1)
}
return nil
}
func branchNeq(v *vm, a, b, c byte) error {
if v.regs[b] != v.regs[c] {
v.code.Seek(int64(v.regs[a]), 1)
}
return nil
}
func branchLess(v *vm, a, b, c byte) error {
if v.regs[b] < v.regs[c] {
v.code.Seek(int64(v.regs[a]), 1)
}
return nil
}
func branchLessEq(v *vm, a, b, c byte) error {
if v.regs[b] <= v.regs[c] {
v.code.Seek(int64(v.regs[a]), 1)
}
return nil
}
func malloc(v *vm, a, b, c byte) error {
addr, err := v.Allocate(word(v.regs[b]))
v.regs[a] = register(addr)
return err
}
func free(v *vm, a, b, c byte) error {
return nil
}
func load(v *vm, a, b, c byte) (err error) {
var tmp word
tmp, err = v.LoadWord(address(v.regs[b]), word(v.regs[c]))
v.regs[a] = register(tmp)
return
}
func loadb(v *vm, a, b, c byte) (err error) {
var tmp byte
tmp, err = v.LoadByte(address(v.regs[b]), word(v.regs[c]))
v.regs[a] = register(tmp)
return
}
func loadi(v *vm, a, b, c byte) error {
v.regs[a] = register(b)<<8 | register(c)
return nil
}
func loadil(v *vm, a, b, c byte) error {
v.regs[a] = register(uint32(v.regs[a]) & 0xFFFF0000)
v.regs[a] |= register(b)<<8 | register(c)
return nil
}
func loadih(v *vm, a, b, c byte) error {
v.regs[a] &= 0x0000FFFF
v.regs[a] |= register(b)<<24 | register(c)<<16
return nil
}
func store(v *vm, a, b, c byte) error {
return v.StoreWord(address(v.regs[b]), word(v.regs[c]), word(v.regs[a]))
}
func storeb(v *vm, a, b, c byte) error {
return v.StoreByte(address(v.regs[b]), word(v.regs[c]), byte(v.regs[a]))
}
func printi(v *vm, a, b, c byte) error {
fmt.Printf("%d", v.regs[a])
return nil
}
func printc(v *vm, a, b, c byte) error {
fmt.Printf("%c", v.regs[a])
return nil
}
func printii(v *vm, a, b, c byte) error {
fmt.Printf("%d", word(b)<<8|word(c))
return nil
}
func printci(v *vm, a, b, c byte) error {
fmt.Printf("%c", word(b)<<8|word(c))
return nil
}