/
machine.cpp
645 lines (544 loc) · 10.9 KB
/
machine.cpp
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
/*
* Made in 2010 by Christian Stigen Larsen
* http://csl.sublevel3.org
*
* Placed in the public domain by the author.
*
*/
#include <stdlib.h>
#include <memory.h>
#include "machine.hpp"
#include "label.hpp"
#include "upper.hpp"
machine_t::machine_t(
const machine_t& p,
void (*error_callback)(const char*))
:
stack(p.stack),
stackip(p.stackip),
labels(p.labels),
memsize(p.memsize),
memory(new int32_t[p.memsize]),
ip(p.ip),
fin(p.fin),
fout(p.fout),
running(p.running),
error_cb(error_callback)
{
memmove(memory, p.memory, memsize*sizeof(int32_t));
}
machine_t::machine_t(const size_t memory_size,
FILE* out,
FILE* in,
void (*error_callback)(const char*))
:
stack(),
stackip(),
labels(),
memsize(memory_size),
memory(new int32_t[memory_size]),
ip(0),
fin(in),
fout(out),
running(true),
error_cb(error_callback)
{
reset();
}
machine_t::machine_t(void (*error_callback)(const char*))
:
stack(),
stackip(),
labels(),
memsize(1000*1024*sizeof(int32_t)),
memory(new int32_t[memsize]),
ip(0),
fin(stdin),
fout(stdout),
running(true),
error_cb(error_callback)
{
reset();
}
machine_t& machine_t::operator=(const machine_t& p)
{
if ( &p == this )
return *this;
delete[](memory);
stack = p.stack;
stackip = p.stackip;
labels = p.labels;
memsize = p.memsize;
memory = new int32_t[memsize];
memcpy(memory, p.memory, memsize*sizeof(int32_t));
ip = p.ip;
fin = p.fin;
fout = p.fout;
running = p.running;
error_cb = p.error_cb;
return *this;
}
void machine_t::reset()
{
memset(memory, NOP, memsize*sizeof(int32_t));
stack.clear();
ip = 0;
}
machine_t::~machine_t()
{
delete[](memory);
}
void machine_t::error(const char* s) const
{
if ( error_cb )
error_cb(s);
}
void machine_t::push(const int32_t& n)
{
stack.push_back(n);
}
void machine_t::puship(const int32_t& n)
{
stackip.push_back(n);
}
int32_t machine_t::popip()
{
if ( stackip.empty() ) {
error("POP empty IP stack");
return 0;
}
int32_t n = stackip.back();
stackip.pop_back();
return n;
}
int32_t machine_t::pop()
{
if ( stack.empty() )
error("POP empty stack");
int32_t n = stack.back();
stack.pop_back();
return n;
}
void machine_t::check_bounds(int32_t n, const char* msg) const
{
if ( n < 0 || static_cast<size_t>(n) >= memsize )
error(msg);
}
void machine_t::next()
{
ip += sizeof(int32_t);
if ( ip < 0 )
error("IP < 0");
if ( static_cast<size_t>(ip) >= memsize )
ip = 0; // TODO: Halt instead of wrap-around?
}
void machine_t::prev()
{
if ( ip == 0 )
error("prev() reached zero");
ip -= sizeof(int32_t);
}
void machine_t::load(Op op)
{
memory[ip] = op;
next();
}
void machine_t::load(int32_t n)
{
memory[ip] = n;
next();
}
int machine_t::run(int32_t start_address)
{
ip = start_address;
while(running)
exec(static_cast<Op>(memory[ip]));
return 0; // TODO: exit-code ?
}
void machine_t::instr_nop()
{
next();
}
void machine_t::instr_add()
{
push(pop() + pop());
next();
}
void machine_t::instr_sub()
{
/*
* This operation is not primitive. It can
* be implemented by adding the minuend to
* the two's complement of the subtrahend:
*
* SUB: ; ( a b -- (b-a))
* swap ; b a
* compl ; b ~a
* 1 add ; b (~a+1), or b -a
* add ; b-a
* popip
*
* The problem is that IF the underlying
* architecture does not use two's complement
* to represent negative values, stuff like
* printing will fail miserably (at least in
* the current implementation on top of C).
*/
// TODO: Consider reversing the operands for SUB
// (it's currently unnatural)
int32_t tos = pop();
push(tos - pop());
next();
}
void machine_t::instr_and()
{
push(pop() & pop());
next();
}
void machine_t::instr_or()
{
push(pop() | pop());
next();
}
void machine_t::instr_xor()
{
push(pop() ^ pop());
next();
}
void machine_t::instr_not()
{
// TODO: this probably does not work as intended
push(!pop());
next();
}
void machine_t::instr_compl()
{
push(~pop());
next();
}
void machine_t::instr_in()
{
/*
* The IN/OUT functions should be implemented
* using something akin to x86 INT or SYSCALL or
* similar. E.g.:
*
* 123 SYSCALL ; exec system call 123
*
*/
push(getc(fin));
next();
}
void machine_t::instr_out()
{
putc(pop(), fout);
fflush(fout);
next();
}
void machine_t::instr_outnum()
{
fprintf(fout, "%u", pop());
next();
}
void machine_t::instr_load()
{
int32_t a = pop();
check_bounds(a, "LOAD");
push(memory[a]);
next();
}
void machine_t::instr_stor()
{
int32_t a = pop();
check_bounds(a, "STOR");
memory[a] = pop();
next();
}
void machine_t::instr_jmp()
{
/*
* This function is not primitive.
* If we have e.g. JZ, we can always
* do "0 JZ" to perform the jump.
*
* (Note that this will break the
* HALT-idiom)
*
*/
// TODO: Implement as library function
//push(0);
//instr_jz();
int32_t a = pop();
check_bounds(a, "JMP");
// check if we are halting, i.e. jumping to current
// address -- if so, quit
if ( a == ip )
running = false;
else
ip = a;
}
void machine_t::instr_jz()
{
int32_t a = pop();
int32_t b = pop();
if ( a != 0 )
next();
else {
check_bounds(b, "JZ");
ip = b; // perform jump
}
}
void machine_t::instr_drop()
{
pop();
next();
}
void machine_t::instr_popip()
{
int32_t a = popip();
check_bounds(a, "POPIP");
ip = a;
}
void machine_t::instr_dropip()
{
popip();
next();
}
void machine_t::instr_jnz()
{
/*
* Only one of JNZ and JZ is needed as
* a primitive -- one can be implemented
* in terms of the other with a negation
* of the TOS.
*
* (Note that this will break the HALT-idiom)
*/
/*
instr_puship();
instr_compl();
instr_popip();
instr_jz();
*/
int32_t a = pop();
int32_t b = pop();
if ( a == 0 )
next();
else {
check_bounds(b, "JNZ");
ip = b; // jump
}
}
void machine_t::instr_push()
{
next();
push(memory[ip]);
next();
}
void machine_t::instr_puship()
{
next();
puship(memory[ip]);
next();
}
void machine_t::instr_dup()
{
/*
* This function is not primitive.
* It can be replaced with a "function":
*
* ; ( a -- a a )
* dup: nop ; placeholder <- nop
* &dup stor ; placeholder <- a
* &dup load ; tos <- a
* &dup load ; tos <- a
* popip
*/
// TODO: Implement as library function
int32_t a = pop();
push(a);
push(a);
next();
}
void machine_t::instr_swap()
{
/*
* This function is not primitive.
* It can be replaced with a "function",
* something like:
*
* ; ( a b -- b a )
* swap:
* swap-b: nop ; placeholder
* swap-a: nop ; placeholder
* &swap-b stor ; swap-b <- b
* &swap-a stor ; swap-a <- a
* &swap-b load ; tos <- a
* &swap-a load ; tos <- b
* popip
*
*/
// TODO: Implement as library function
// a, b -- b, a
int32_t b = pop();
int32_t a = pop();
push(b);
push(a);
next();
}
void machine_t::instr_rol3()
{
/*
* This function is not primitive.
* It can be replaced with "functions",
* something like:
*
* rol3:
* rol3-var: nop ; stack = a b c
* &rol3-var stor ; stack = a b, var = c
* swap ; stack = b a, var = c
* &rol3-var load ; stack = b a c
* swap ; stack = b c a
* popip
*
*/
// TODO: Implement as library function
// abc -> bca
int32_t c = pop(); // TOS
int32_t b = pop();
int32_t a = pop();
push(b);
push(c);
push(a);
next();
}
void machine_t::exec(Op operation)
{
switch(operation) {
default: error("Unknown instruction"); break;
case NOP: instr_nop(); break;
// Strictly speaking, SUB can be implemented
// by ADDing the minuend with the two's complement
// of the subtrahend -- but that's not necessarily
// portable down to native code
case ADD: instr_add(); break;
case SUB: instr_sub(); break; // non-primitive
// Strictly speaking, all but NOT and AND are
// non-primitive (or some other combination of
// two operations)
case AND: instr_and(); break;
case OR: instr_or(); break;
case XOR: instr_xor(); break;
case NOT: instr_not(); break;
case COMPL: instr_compl(); break;
// Should be replaced with x86 INT-like operations
case IN: instr_in(); break;
case OUT: instr_out(); break;
case LOAD: instr_load(); break;
case STOR: instr_stor(); break;
case PUSH: instr_push(); break;
case DROP: instr_drop(); break;
case PUSHIP: instr_puship(); break;
case POPIP: instr_popip(); break;
case DROPIP: instr_dropip(); break;
case JZ: instr_jz(); break;
case JMP: instr_jmp(); break; // non-primitive
case JNZ: instr_jnz(); break; // non-primitive
case DUP: instr_dup(); break; // non-primitive
case SWAP: instr_swap(); break; // non-primitive
case ROL3: instr_rol3(); break; // non-primitive
case OUTNUM: instr_outnum(); break; // non-primitive
}
}
int32_t* machine_t::find_end() const
{
// find end of program by scanning
// backwards until non-NOP is found
int32_t *p = &memory[memsize-1];
while ( *p == NOP ) --p;
return p;
}
void machine_t::load_image(FILE* f)
{
reset();
while ( !feof(f) ) {
Op op = NOP;
fread(&op, sizeof(Op), 1, f);
load(op);
}
ip = 0;
}
void machine_t::save_image(FILE* f) const
{
int32_t *start = memory;
int32_t *end = find_end() + sizeof(int32_t);
while ( start != end ) {
fwrite(start, sizeof(Op), 1, f);
start += sizeof(int32_t);
}
}
void machine_t::load_halt()
{
load(PUSH);
load(ip + sizeof(int32_t));
load(JMP);
}
size_t machine_t::size() const
{
return find_end() - &memory[0];
}
int32_t machine_t::cur() const
{
return memory[ip];
}
int32_t machine_t::pos() const
{
return ip;
}
void machine_t::addlabel(const char* name, int32_t pos, int)
{
std::string n = upper(name);
if ( n.empty() )
error("Empty label");
else {
n.erase(n.length()-1, 1); // remove ":"
labels.push_back(label_t(n.c_str(), pos));
}
}
int32_t machine_t::get_label_address(const std::string& s) const
{
std::string p(upper(s));
// special label address "here" returns current position
if ( p == "HERE" )
return ip;
for ( size_t n=0; n < labels.size(); ++n )
if ( upper(labels[n].name.c_str()) == p )
return labels[n].pos;
return -1; // not found
}
bool machine_t::isrunning() const
{
return running;
}
void machine_t::set_fout(FILE* f)
{
fout = f;
}
void machine_t::set_fin(FILE* f)
{
fin = f;
}
void machine_t::set_mem(int32_t adr, int32_t val)
{
check_bounds(adr, "set_mem out of bounds");
memory[adr] = val;
}
int32_t machine_t::get_mem(int32_t adr) const
{
check_bounds(adr, "get_mem out of bounds");
return memory[adr];
}
int32_t machine_t::wordsize() const
{
return sizeof(int32_t);
}