-
Notifications
You must be signed in to change notification settings - Fork 4
/
HeaderGenerator.java
executable file
·192 lines (161 loc) · 5.71 KB
/
HeaderGenerator.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
package com.badlogic.jack.generators;
import soot.SootClass;
import soot.SootField;
import soot.SootMethod;
import soot.Type;
import soot.VoidType;
import com.badlogic.jack.build.FileDescriptor;
import com.badlogic.jack.info.ClassInfo;
import com.badlogic.jack.info.FieldInfo;
import com.badlogic.jack.info.MethodInfo;
import com.badlogic.jack.info.SyntheticMethodInfo;
import com.badlogic.jack.utils.CTypes;
import com.badlogic.jack.utils.Mangling;
import com.badlogic.jack.utils.SourceWriter;
/**
* Takes a {@link SootClass} instance and a corresponding {@link ClassInfo}
* instance and outputs a C++ header file
* @author mzechner
*
*/
public class HeaderGenerator {
final SootClass clazz;
final ClassInfo info;
final String fileName;
final SourceWriter writer;
public HeaderGenerator(SootClass clazz, ClassInfo info, String fileName) {
this.clazz = clazz;
this.info = info;
this.fileName = fileName;
this.writer = new SourceWriter();
}
public void generate() {
System.out.println("generating header for " + clazz.getName());
// include guards
writer.wl("#ifndef " + info.mangledName + "_h");
writer.wl("#define " + info.mangledName + "_h");
writer.wl("");
// if this is java.lang.Object, we need to output
// header includes for the Boehm GC
if(clazz.getName().equals("java.lang.Object")) {
writer.wl("#define GC_THREADS");
writer.wl("#define GC_NOT_DLL");
writer.wl("#include <gc_cpp.h>");
}
// include common headers
writer.wl("#include \"vm/types.h\"");
writer.wl("");
// emit forward declarations
for(String forwardDecl: info.forwardedClasses) {
writer.wl("class " + forwardDecl + ";");
}
writer.wl("template <class T> class Array;");
// emit superclass, interface and covariant return type includes
for(String includedClass: info.includedClasses) {
writer.wl("#include \"classes/" + includedClass + ".h\"");
}
writer.wl("");
// emit class signature
String signature = "class " + info.mangledName + ": ";
signature += "public virtual " + info.superClass;
for(String itf: info.interfaces) {
signature += ", public virtual " + itf;
}
writer.wl(signature + " {");
writer.push();
writer.wl("public:" );
writer.push();
// emit fields (in the order they appear in the class file)
for(SootField field: clazz.getFields()) {
FieldInfo fieldInfo = info.fieldInfos.get(field);
writer.wl((field.isStatic()?"static ": "") + fieldInfo.cType + " " + fieldInfo.mangledName + ";");
}
// add in a static field keeping track of whether clinit was called
// and another static field for the class.
writer.wl("static java_lang_Class* clazz;");
writer.wl("static bool clinit;");
writer.wl("");
// emit methods (in the order they appear in the class file)
for(SootMethod method: clazz.getMethods()) {
MethodInfo methodInfo = info.methodInfos.get(method);
if(methodInfo == null) {
System.out.println("skipping method " + method);
continue;
}
generateMethod(method, methodInfo);
}
// emit synthetic methods
for(SyntheticMethodInfo syntheticMethod: info.syntheticMethods) {
generateSyntheticMethod(syntheticMethod);
}
// emit getClass() implementation
writer.wl("virtual java_lang_Class* m_getClass() { return " + Mangling.mangle(clazz) + "::clazz; }");
// if we don't have a <clinit> declaration, create one!
if(!info.hasClinit) {
writer.wl("static void m_clinit();");
}
writer.pop();
writer.pop();
writer.wl("};");
writer.wl("#endif");
// only update header if content changed
if(needsUpdate(fileName, writer.toString())) {
new FileDescriptor(fileName).writeString(writer.toString(), false);
} else {
System.out.println("Skipped '" + fileName + "', up-to-date");
}
}
private boolean needsUpdate(String fileName, String newContent) {
FileDescriptor file = new FileDescriptor(fileName);
if(!file.exists()) return true;
return !file.readString().equals(newContent);
}
private void generateMethod(SootMethod method, MethodInfo info) {
SootClass clazz = method.getDeclaringClass();
String methodSig = "";
if(method.isStatic()) {
methodSig +="static ";
} else {
methodSig +="virtual ";
}
methodSig += CTypes.toCType(method.getReturnType());
methodSig += " " + Mangling.mangle(method) + "(";
int i = 0;
for(Object paramType: method.getParameterTypes()) {
if(i > 0) methodSig += ", ";
methodSig += CTypes.toCType((Type)paramType);
methodSig += " param" + i;
i++;
}
methodSig +=")";
if(clazz.isInterface() || method.isAbstract()) methodSig += " = 0";
methodSig += ";";
writer.wl(methodSig);
}
public void generateSyntheticMethod(SyntheticMethodInfo syntheticMethod) {
String methodSig = "";
SootMethod baseMethod = syntheticMethod.baseMethod;
methodSig +="virtual ";
methodSig += syntheticMethod.returnType;
methodSig += " " + Mangling.mangle(baseMethod) + "(";
int i = 0;
for(Object paramType: baseMethod.getParameterTypes()) {
if(i > 0) methodSig += ", ";
methodSig += CTypes.toCType((Type)paramType);
methodSig += " param" + i;
i++;
}
boolean hasReturnType = !(baseMethod.getReturnType() instanceof VoidType);
if(!syntheticMethod.isPure) {
methodSig +=") { " + (hasReturnType?"return ":"") + Mangling.mangle(baseMethod.getDeclaringClass()) + "::" + Mangling.mangle(baseMethod) + "(";
for(i = 0; i < baseMethod.getParameterTypes().size(); i++) {
if(i > 0) methodSig += ", ";
methodSig += " param" + i;
}
methodSig += "); };";
} else {
methodSig +=") = 0;";
}
writer.wl(methodSig);
}
}