Skip to content

Commit a60a6da

Browse files
committed
compile ok
1 parent 0dfd655 commit a60a6da

File tree

10 files changed

+313
-155
lines changed

10 files changed

+313
-155
lines changed

build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
echo need : make clang llvm-as llc java
2+
3+
echo compile java source *.java and convert to llvm ir file *.ll
4+
java -cp ./class2ir/dist/class2ir.jar:./class2ir/lib/asm-7.2.jar:./class2ir/lib/classparser.jar j2ll.Main ./app/java ./app/out/classes ./app/c/
5+
6+
cd ./app/c/
7+
make
8+
cp ./c ../../app.exe
9+
make clean
10+
cd ../../
11+
rm -rf ./app/out/classes/*
12+
rm -rf ./class2ir/target
13+
echo success

class2ir/dist/class2ir.jar

2.1 KB
Binary file not shown.

class2ir/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
<maven.compiler.target>1.8</maven.compiler.target>
1515
</properties>
1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>org.objectweb.asm</groupId>
20+
<artifactId>asm</artifactId>
21+
<version>7.2.0</version>
22+
<scope>system</scope>
23+
<systemPath>${project.basedir}/lib/asm-7.2.jar</systemPath>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.mini</groupId>
27+
<artifactId>classparser</artifactId>
28+
<version>7.2</version>
29+
<scope>system</scope>
30+
<systemPath>${project.basedir}/lib/classparser.jar</systemPath>
31+
</dependency>
32+
</dependencies>
33+
1734
<build>
1835
<finalName>${project.artifactId}</finalName>
1936
<plugins>

class2ir/src/main/java/j2ll/LocalVarTable.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package j2ll;
22

3-
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
4-
import jdk.internal.org.objectweb.asm.Opcodes;
3+
import org.objectweb.asm.Opcodes;
54

65
import java.util.*;
76

class2ir/src/main/java/j2ll/MV.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,33 +171,36 @@ public void visitCode() {
171171
@Override
172172
public void visitFrame(int type, int numLocal, Object[] local, int numStack, Object[] stackitems) {
173173
//out.add("; TODO FRame: " + i + " " + i1 + " " + i2);
174-
out.add("; type " + type + ", local " + numLocal + " " + Arrays.toString(local) + "," + " stack " + numStack + " " + Arrays.toString(stackitems));
174+
//out.add("; type " + type + ", local " + numLocal + " " + Arrays.toString(local) + "," + " stack " + numStack + " " + Arrays.toString(stackitems));
175175
vars.activeByFrame(type, numLocal, local);
176176

177177
switch (type) {
178-
case jdk.internal.org.objectweb.asm.Opcodes.F_APPEND: { //1
178+
case org.objectweb.asm.Opcodes.F_APPEND: { //1
179179

180180
break;
181181
}
182-
case jdk.internal.org.objectweb.asm.Opcodes.F_CHOP: {//2
182+
case org.objectweb.asm.Opcodes.F_CHOP: {//2
183183

184184
break;
185185
}
186-
case jdk.internal.org.objectweb.asm.Opcodes.F_FULL: {//0
186+
case org.objectweb.asm.Opcodes.F_FULL: {//0
187187
//System.out.println("expect:" + numStack + " real:" + stack.size());
188-
while (stack.size() > numStack) stack.pop();
188+
while (stack.size() > numStack) {
189+
StackValue sv = stack.pop();
190+
out.add(";pop a var from stack NEED FIX :" + sv.fullName());
191+
}
189192
if (stack.size() < numStack) {
190193
throw new RuntimeException("todo gust");
191194
}
192195
break;
193196
}
194-
case jdk.internal.org.objectweb.asm.Opcodes.F_NEW: {//-1
197+
case org.objectweb.asm.Opcodes.F_NEW: {//-1
195198
break;
196199
}
197-
case jdk.internal.org.objectweb.asm.Opcodes.F_SAME: {//3
200+
case org.objectweb.asm.Opcodes.F_SAME: {//3
198201
break;
199202
}
200-
case jdk.internal.org.objectweb.asm.Opcodes.F_SAME1: {//4
203+
case org.objectweb.asm.Opcodes.F_SAME1: {//4
201204
break;
202205
}
203206
default: {
@@ -1115,7 +1118,7 @@ public void out(PrintStream ps) {
11151118
result = ms.split("\n");
11161119
discardDoubleLabel(result);
11171120

1118-
IrFunction irf = new IrFunction();
1121+
IrFunction irf = new IrFunction(cv.className, methodName, javaSignature);
11191122
irf.define = define;
11201123
irf.end = "}";
11211124
irf.parse(result);

class2ir/src/main/java/j2ll/Main.java

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,68 @@ public class Main {
1515

1616

1717
public static void main(String[] args) throws IOException {
18-
String srcPath = "../app/java/";
19-
String distPath = "../app/out/classes/";
20-
File f = new File(distPath);
18+
String srcPath = "./app/java/";
19+
String classesPath = "./app/out/classes/";
20+
String llvmPath = "./app/c/";
21+
22+
23+
if (args.length < 3) {
24+
System.out.println("Posix :");
25+
System.out.println("Compile java file:");
26+
System.out.println("java -cp ./class2ir/dist/class2ir.jar:./class2ir/lib/asm-7.2.jar:./class2ir/lib/classparser.jar j2ll.Main ./app/java ./app/out/classes ./app/c/");
27+
} else {
28+
srcPath = args[0] + "/";
29+
classesPath = args[1] + "/";
30+
llvmPath = args[2] + "/";
31+
}
32+
33+
System.out.println("java source *.java path : " + srcPath);
34+
System.out.println("classes *.class output path : " + classesPath);
35+
System.out.println("llvm *.ll output path : " + llvmPath);
36+
37+
compile(srcPath, classesPath);
38+
39+
convert2ll("java.lang.Object", classesPath, llvmPath);
40+
convert2ll("java.io.PrintStream", classesPath, llvmPath);
41+
convert2ll("java.lang.System", classesPath, llvmPath);
42+
convert2ll("java.lang.Throwable", classesPath, llvmPath);
43+
convert2ll("java.lang.NullPointerException", classesPath, llvmPath);
44+
convert2ll("java.lang.String", classesPath, llvmPath);
45+
convert2ll("java.lang.StringBuilder", classesPath, llvmPath);
46+
convert2ll("test.Test", classesPath, llvmPath);
47+
convert2ll("test.TestParent", classesPath, llvmPath);
48+
49+
}
50+
51+
static void compile(String srcPath, String classesPath) {
52+
53+
File f = new File(classesPath);
2154
if (!f.exists()) {
2255
f.mkdirs();
2356
}
24-
compiler.MyCompiler.compile(srcPath, distPath);
57+
compiler.MyCompiler.compile(srcPath, classesPath);
2558
List<String> files = new ArrayList<>();
26-
MyCompiler.find(distPath, files, null, ".class");
59+
MyCompiler.find(classesPath, files, null, ".class");
2760

2861
Util.helper = new ClassHelper("", files.toArray(new String[files.size()]));
2962
Util.helper.openClasses();
3063

31-
conv("java.lang.Object", distPath);
32-
conv("java.io.PrintStream", distPath);
33-
conv("java.lang.System", distPath);
34-
conv("java.lang.Throwable", distPath);
35-
conv("java.lang.NullPointerException", distPath);
36-
conv("java.lang.String", distPath);
37-
conv("java.lang.StringBuilder", distPath);
38-
conv("test.Test", distPath);
39-
conv("test.TestParent", distPath);
40-
4164
}
4265

66+
static void convert2ll(String className, String classesPath, String llvmPath) throws IOException {
4367

44-
static void conv(String className, String home) throws IOException {
45-
46-
String out = className + ".ll";
47-
PrintStream ps = new PrintStream(new File("../app/c/", out));
68+
String outFileName = className + ".ll";
69+
PrintStream ps = new PrintStream(new File(llvmPath, outFileName));
4870

4971
Statistics statistics = new Statistics();
5072
StatisticsCollector sc = new StatisticsCollector(statistics);
5173
CV cv = new CV(ps, statistics);
5274

5375
// read class
54-
String prefix = home;
55-
String fn = prefix + className.replace('.', '/') + ".class";
76+
String fn = classesPath + className.replace('.', '/') + ".class";
5677
System.out.println("class convert to llvm ir:" + fn);
5778
InputStream is = new FileInputStream(fn);
5879
ClassReader cr = new ClassReader(is);
59-
// ClassReader cr = new ClassReader(className);
6080
cr.accept(sc, 0);
6181

6282

0 commit comments

Comments
 (0)