-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachine.java
220 lines (198 loc) · 7.04 KB
/
Machine.java
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
/* File MicroC/Machine.java
A unified-stack abstract machine for imperative programs.
[email protected] * 2001-03-21, 2009-09-24
To execute a program file using this abstract machine, do:
java Machine <programfile> <arg1> <arg2> ...
or, to get a trace of the program execution:
java Machinetrace <programfile> <arg1> <arg2> ...
*/
import java.io.*;
import java.util.*;
class Machine {
public static void main(String[] args)
throws FileNotFoundException, IOException {
if (args.length == 0)
System.out.println("Usage: java Machine <programfile> <arg1> ...\n");
else
execute(args, false);
}
// These numeric instruction codes must agree with Machine.fs:
final static int
CSTI = 0, ADD = 1, SUB = 2, MUL = 3, DIV = 4, MOD = 5,
EQ = 6, LT = 7, NOT = 8,
DUP = 9, SWAP = 10,
LDI = 11, STI = 12,
GETBP = 13, GETSP = 14, INCSP = 15,
GOTO = 16, IFZERO = 17, IFNZRO = 18, CALL = 19, TCALL = 20, RET = 21,
PRINTI = 22, PRINTC = 23,
LDARGS = 24,
STOP = 25;
final static int STACKSIZE = 1000;
// Read code from file and execute it
static void execute(String[] args, boolean trace)
throws FileNotFoundException, IOException {
int[] p = readfile(args[0]); // Read the program from file
int[] s = new int[STACKSIZE]; // The evaluation stack
int[] iargs = new int[args.length-1];
for (int i=1; i<args.length; i++) // Push commandline arguments
iargs[i-1] = Integer.parseInt(args[i]);
long starttime = System.currentTimeMillis();
execcode(p, s, iargs, trace); // Execute program proper
long runtime = System.currentTimeMillis() - starttime;
System.err.println("\nRan " + runtime/1000.0 + " seconds");
}
// The machine: execute the code starting at p[pc]
static int execcode(int[] p, int[] s, int[] iargs, boolean trace) {
int bp = -999; // Base pointer, for local variable access
int sp = -1; // Stack top pointer
int pc = 0; // Program counter: next instruction
for (;;) {
if (trace)
printsppc(s, bp, sp, p, pc);
switch (p[pc++]) {
case CSTI:
s[sp+1] = p[pc++]; sp++; break;
case ADD:
s[sp-1] = s[sp-1] + s[sp]; sp--; break;
case SUB:
s[sp-1] = s[sp-1] - s[sp]; sp--; break;
case MUL:
s[sp-1] = s[sp-1] * s[sp]; sp--; break;
case DIV:
s[sp-1] = s[sp-1] / s[sp]; sp--; break;
case MOD:
s[sp-1] = s[sp-1] % s[sp]; sp--; break;
case EQ:
s[sp-1] = (s[sp-1] == s[sp] ? 1 : 0); sp--; break;
case LT:
s[sp-1] = (s[sp-1] < s[sp] ? 1 : 0); sp--; break;
case NOT:
s[sp] = (s[sp] == 0 ? 1 : 0); break;
case DUP:
s[sp+1] = s[sp]; sp++; break;
case SWAP:
{ int tmp = s[sp]; s[sp] = s[sp-1]; s[sp-1] = tmp; } break;
case LDI: // load indirect
s[sp] = s[s[sp]]; break;
case STI: // store indirect, keep value on top
s[s[sp-1]] = s[sp]; s[sp-1] = s[sp]; sp--; break;
case GETBP:
s[sp+1] = bp; sp++; break;
case GETSP:
s[sp+1] = sp; sp++; break;
case INCSP:
sp = sp+p[pc++]; break;
case GOTO:
pc = p[pc]; break;
case IFZERO:
pc = (s[sp--] == 0 ? p[pc] : pc+1); break;
case IFNZRO:
pc = (s[sp--] != 0 ? p[pc] : pc+1); break;
case CALL: {
int argc = p[pc++];
for (int i=0; i<argc; i++) // Make room for return address
s[sp-i+2] = s[sp-i]; // and old base pointer
s[sp-argc+1] = pc+1; sp++;
s[sp-argc+1] = bp; sp++;
bp = sp+1-argc;
pc = p[pc];
} break;
case TCALL: {
int argc = p[pc++]; // Number of new arguments
int pop = p[pc++]; // Number of variables to discard
for (int i=argc-1; i>=0; i--) // Discard variables
s[sp-i-pop] = s[sp-i];
sp = sp - pop; pc = p[pc];
} break;
case RET: {
int res = s[sp];
sp = sp-p[pc]; bp = s[--sp]; pc = s[--sp];
s[sp] = res;
} break;
case PRINTI:
System.out.print(s[sp] + " "); break;
case PRINTC:
System.out.print((char)(s[sp])); break;
case LDARGS:
for (int i=0; i<iargs.length; i++) // Push commandline arguments
s[++sp] = iargs[i];
break;
case STOP:
return sp;
default:
throw new RuntimeException("Illegal instruction " + p[pc-1]
+ " at address " + (pc-1));
}
}
}
// Print the stack machine instruction at p[pc]
static String insname(int[] p, int pc) {
switch (p[pc]) {
case CSTI: return "CSTI " + p[pc+1];
case ADD: return "ADD";
case SUB: return "SUB";
case MUL: return "MUL";
case DIV: return "DIV";
case MOD: return "MOD";
case EQ: return "EQ";
case LT: return "LT";
case NOT: return "NOT";
case DUP: return "DUP";
case SWAP: return "SWAP";
case LDI: return "LDI";
case STI: return "STI";
case GETBP: return "GETBP";
case GETSP: return "GETSP";
case INCSP: return "INCSP " + p[pc+1];
case GOTO: return "GOTO " + p[pc+1];
case IFZERO: return "IFZERO " + p[pc+1];
case IFNZRO: return "IFNZRO " + p[pc+1];
case CALL: return "CALL " + p[pc+1] + " " + p[pc+2];
case TCALL: return "TCALL " + p[pc+1] + " " + p[pc+2] + " " + p[pc+3];
case RET: return "RET " + p[pc+1];
case PRINTI: return "PRINTI";
case PRINTC: return "PRINTC";
case LDARGS: return "LDARGS";
case STOP: return "STOP";
default: return "<unknown>";
}
}
// Print current stack and current instruction
static void printsppc(int[] s, int bp, int sp, int[] p, int pc) {
System.out.print("[ ");
for (int i=0; i<=sp; i++)
System.out.print(s[i] + " ");
System.out.print("]");
System.out.println("{" + pc + ": " + insname(p, pc) + "}");
}
// Read instructions from a file
public static int[] readfile(String filename)
throws FileNotFoundException, IOException
{
ArrayList<Integer> rawprogram = new ArrayList<Integer>();
Reader inp = new FileReader(filename);
StreamTokenizer tstream = new StreamTokenizer(inp);
tstream.parseNumbers();
tstream.nextToken();
while (tstream.ttype == StreamTokenizer.TT_NUMBER) {
rawprogram.add(new Integer((int)tstream.nval));
tstream.nextToken();
}
inp.close();
final int programsize = rawprogram.size();
int[] program = new int[programsize];
for (int i=0; i<programsize; i++)
program[i] = ((Integer)(rawprogram.get(i))).intValue();
return program;
}
}
// Run the machine with tracing: print each instruction as it is executed
class Machinetrace {
public static void main(String[] args)
throws FileNotFoundException, IOException {
if (args.length == 0)
System.out.println("Usage: java Machinetrace <programfile> <arg1> ...\n");
else
Machine.execute(args, true);
}
}