-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.java
More file actions
76 lines (66 loc) · 2.34 KB
/
CodeGenerator.java
File metadata and controls
76 lines (66 loc) · 2.34 KB
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
import java.io.*;
import java.util.LinkedList;
public class CodeGenerator {
LinkedList<Instruction> instructions = new LinkedList<Instruction>();
int label = 0;
public void emit(OpCode opCode) { instructions.add(new Instruction(opCode)); }
public void emit(OpCode opCode, int operand) {
instructions.add(new Instruction(opCode, operand));
}
public void emitLabel(int operand) { emit(OpCode.label, operand); }
public int newLabel() { return label++; }
public void toJasmin() throws IOException {
PrintWriter out = new PrintWriter(new FileWriter("Output.j"));
String temp = "";
temp = temp + header;
while (instructions.size() > 0) {
Instruction tmp = instructions.remove();
temp = temp + tmp.toJasmin();
}
temp = temp + footer;
out.println(temp);
out.flush();
out.close();
}
private static final String header =
".class public Output \n"
+ ".super java/lang/Object\n"
+ "\n"
+ ".method public <init>()V\n"
+ " aload_0\n"
+ " invokenonvirtual java/lang/Object/<init>()V\n"
+ " return\n"
+ ".end method\n"
+ "\n"
+ ".method public static print(I)V\n"
+ " .limit stack 2\n"
+ " getstatic java/lang/System/out Ljava/io/PrintStream;\n"
+ " iload_0 \n"
+ " invokestatic java/lang/Integer/toString(I)Ljava/lang/String;\n"
+ " invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n"
+ " return\n"
+ ".end method\n"
+ "\n"
+ ".method public static read()I\n"
+ " .limit stack 3\n"
+ " new java/util/Scanner\n"
+ " dup\n"
+ " getstatic java/lang/System/in Ljava/io/InputStream;\n"
+ " invokespecial java/util/Scanner/<init>(Ljava/io/InputStream;)V\n"
+ " invokevirtual java/util/Scanner/next()Ljava/lang/String;\n"
+ " invokestatic java/lang/Integer.parseInt(Ljava/lang/String;)I\n"
+ " ireturn\n"
+ ".end method\n"
+ "\n"
+ ".method public static run()V\n"
+ " .limit stack 1024\n"
+ " .limit locals 256\n";
private static final String footer =
" return\n"
+ ".end method\n"
+ "\n"
+ ".method public static main([Ljava/lang/String;)V\n"
+ " invokestatic Output/run()V\n"
+ " return\n"
+ ".end method\n";
}