提交 39ac3dc3 authored 作者: Thomas Mueller's avatar Thomas Mueller

A Java parser / Java to C converter.

上级 04db103b
...@@ -47,7 +47,7 @@ class CallExpr implements Expr { ...@@ -47,7 +47,7 @@ class CallExpr implements Expr {
MethodObj m = classObj.getMethod(name, args); MethodObj m = classObj.getMethod(name, args);
String methodName; String methodName;
if (m.isVirtual) { if (m.isVirtual) {
methodName = "*virtual_" + m.name + "[CLASS_ID("+expr.toString()+")]"; methodName = "virtual_" + m.name + "[CLASS_ID("+expr.toString()+")]";
} else { } else {
methodName = JavaParser.toC(classObj.toString() + "." + m.name); methodName = JavaParser.toC(classObj.toString() + "." + m.name);
} }
...@@ -224,7 +224,7 @@ class NewExpr implements Expr { ...@@ -224,7 +224,7 @@ class NewExpr implements Expr {
buff.append(")"); buff.append(")");
} }
} else { } else {
buff.append("NEW_OBJ(" + type.id + ", " + type + ")"); buff.append("NEW_OBJ(" + type.id + ", " + JavaParser.toC(type.toString()) + ")");
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -28,10 +28,10 @@ public class JavaParser { ...@@ -28,10 +28,10 @@ public class JavaParser {
private static final int TOKEN_OTHER = 5; private static final int TOKEN_OTHER = 5;
private static final HashSet<String> RESERVED = new HashSet<String>(); private static final HashSet<String> RESERVED = new HashSet<String>();
private static final HashMap<String, ClassObj> BUILT_IN_TYPES = new HashMap<String, ClassObj>();
private static final HashMap<String, String> JAVA_IMPORT_MAP = new HashMap<String, String>(); private static final HashMap<String, String> JAVA_IMPORT_MAP = new HashMap<String, String>();
private static int firstClassId; private final ArrayList<ClassObj> allClasses = new ArrayList<ClassObj>();
private final HashMap<String, ClassObj> builtInTypes = new HashMap<String, ClassObj>();
private String source; private String source;
...@@ -39,7 +39,7 @@ public class JavaParser { ...@@ -39,7 +39,7 @@ public class JavaParser {
private String packageName; private String packageName;
private ClassObj classObj; private ClassObj classObj;
private int classId = firstClassId; private int nextClassId;
private MethodObj method; private MethodObj method;
private FieldObj thisPointer; private FieldObj thisPointer;
private HashMap<String, String> importMap = new HashMap<String, String>(); private HashMap<String, String> importMap = new HashMap<String, String>();
...@@ -49,7 +49,11 @@ public class JavaParser { ...@@ -49,7 +49,11 @@ public class JavaParser {
private ArrayList<Statement> nativeHeaders = new ArrayList<Statement>(); private ArrayList<Statement> nativeHeaders = new ArrayList<Statement>();
static { public JavaParser() {
addBuiltInTypes();
}
private void addBuiltInTypes() {
String[] list = { "abstract", "continue", "for", "new", "switch", "assert", "default", "if", String[] list = { "abstract", "continue", "for", "new", "switch", "assert", "default", "if",
"package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements", "package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements",
"protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof",
...@@ -76,16 +80,25 @@ public class JavaParser { ...@@ -76,16 +80,25 @@ public class JavaParser {
JAVA_IMPORT_MAP.put(s, "java.lang." + s); JAVA_IMPORT_MAP.put(s, "java.lang." + s);
addBuiltInType(id++, false, 0, "java.lang." + s); addBuiltInType(id++, false, 0, "java.lang." + s);
} }
firstClassId = id; nextClassId = id;
}
private void addBuiltInType(int id, boolean primitive, int primitiveType, String type) {
ClassObj c = new ClassObj();
c.id = id;
c.name = type;
c.isPrimitive = primitive;
c.primitiveType = primitiveType;
builtInTypes.put(type, c);
addClass(c);
} }
private static void addBuiltInType(int id, boolean primitive, int primitiveType, String type) { private void addClass(ClassObj c) {
ClassObj typeObj = new ClassObj(); int id = c.id;
typeObj.id = id; while (id >= allClasses.size()) {
typeObj.name = type; allClasses.add(null);
typeObj.isPrimitive = primitive; }
typeObj.primitiveType = primitiveType; allClasses.set(id, c);
BUILT_IN_TYPES.put(type, typeObj);
} }
/** /**
...@@ -151,16 +164,17 @@ public class JavaParser { ...@@ -151,16 +164,17 @@ public class JavaParser {
isInterface = true; isInterface = true;
} }
String name = readIdentifier(); String name = readIdentifier();
classObj = BUILT_IN_TYPES.get(packageName + "." + name); classObj = builtInTypes.get(packageName + "." + name);
if (classObj == null) { if (classObj == null) {
classObj = new ClassObj(); classObj = new ClassObj();
classObj.id = classId++; classObj.id = nextClassId++;
} }
classObj.isPublic = isPublic; classObj.isPublic = isPublic;
classObj.isInterface = isInterface; classObj.isInterface = isInterface;
classObj.name = packageName == null ? "" : (packageName + ".") + name; classObj.name = packageName == null ? "" : (packageName + ".") + name;
// import this class // import this class
importMap.put(name, classObj.name); importMap.put(name, classObj.name);
addClass(classObj);
classes.put(classObj.name, classObj); classes.put(classObj.name, classObj);
if (readIf("extends")) { if (readIf("extends")) {
classObj.superClassName = readQualifiedIdentifier(); classObj.superClassName = readQualifiedIdentifier();
...@@ -181,7 +195,7 @@ public class JavaParser { ...@@ -181,7 +195,7 @@ public class JavaParser {
} }
private boolean isTypeOrIdentifier() { private boolean isTypeOrIdentifier() {
if (BUILT_IN_TYPES.containsKey(current.token)) { if (builtInTypes.containsKey(current.token)) {
return true; return true;
} }
return current.type == TOKEN_IDENTIFIER; return current.type == TOKEN_IDENTIFIER;
...@@ -196,7 +210,7 @@ public class JavaParser { ...@@ -196,7 +210,7 @@ public class JavaParser {
} }
private ClassObj getClassIf(String type) { private ClassObj getClassIf(String type) {
ClassObj c = BUILT_IN_TYPES.get(type); ClassObj c = builtInTypes.get(type);
if (c != null) { if (c != null) {
return c; return c;
} }
...@@ -213,7 +227,7 @@ public class JavaParser { ...@@ -213,7 +227,7 @@ public class JavaParser {
} }
c = classes.get(mappedType); c = classes.get(mappedType);
if (c == null) { if (c == null) {
c = BUILT_IN_TYPES.get(mappedType); c = builtInTypes.get(mappedType);
if (c == null) { if (c == null) {
throw new RuntimeException("Unknown class: " + mappedType); throw new RuntimeException("Unknown class: " + mappedType);
} }
...@@ -409,7 +423,7 @@ public class JavaParser { ...@@ -409,7 +423,7 @@ public class JavaParser {
private String readTypeOrIdentifier() { private String readTypeOrIdentifier() {
if (current.type == TOKEN_RESERVED) { if (current.type == TOKEN_RESERVED) {
if (BUILT_IN_TYPES.containsKey(current.token)) { if (builtInTypes.containsKey(current.token)) {
return read(); return read();
} }
} }
...@@ -745,7 +759,7 @@ public class JavaParser { ...@@ -745,7 +759,7 @@ public class JavaParser {
expr = e2; expr = e2;
if (n.equals("length") && t.arrayLevel > 0) { if (n.equals("length") && t.arrayLevel > 0) {
e2.field = new FieldObj(); e2.field = new FieldObj();
e2.field.type = BUILT_IN_TYPES.get("int").baseType; e2.field.type = builtInTypes.get("int").baseType;
e2.field.name = "length"; e2.field.name = "length";
} else { } else {
if (t == null || t.classObj == null) { if (t == null || t.classObj == null) {
...@@ -1380,11 +1394,11 @@ public class JavaParser { ...@@ -1380,11 +1394,11 @@ public class JavaParser {
i++; i++;
} }
out.println(") = {"); out.println(") = {");
for (ClassObj c : classes.values()) { for (ClassObj c : allClasses) {
if (c.methods.containsKey(m.name)) { if (c != null && c.methods.containsKey(m.name)) {
out.println(" " + toC(c.name) + "_" + m.name + ", "); out.println(toC(c.name) + "_" + m.name + ", ");
} else { } else {
out.println(" 0, "); out.print("0, ");
} }
} }
out.println("};"); out.println("};");
...@@ -1494,7 +1508,7 @@ public class JavaParser { ...@@ -1494,7 +1508,7 @@ public class JavaParser {
* @return the class * @return the class
*/ */
ClassObj getClassObj(String className) { ClassObj getClassObj(String className) {
ClassObj c = BUILT_IN_TYPES.get(className); ClassObj c = builtInTypes.get(className);
if (c == null) { if (c == null) {
c = classes.get(className); c = classes.get(className);
} }
......
...@@ -25,8 +25,7 @@ int main(int argc, char** argv) { ...@@ -25,8 +25,7 @@ int main(int argc, char** argv) {
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String... args) { public static void main(String... args) {
System.out.println("Hello World"); System.out.println("Hello " + "World" + 1);
System.out.println("Hello!");
} }
public int hashCode() { public int hashCode() {
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.java.lang;
/**
* A java.lang.String implementation.
*/
public class Math {
/**
* Get the larger of both values.
*
* @param a the first value
* @param b the second value
* @return the larger
*/
public static int max(int a, int b) {
return a > b ? a : b;
}
}
...@@ -28,9 +28,10 @@ package org.h2.java.lang; ...@@ -28,9 +28,10 @@ package org.h2.java.lang;
#define false 0 #define false 0
#define null 0 #define null 0
#define LENGTH(a) (*(((jint*)(a))-2)) #define LENGTH(a) (*(((jint*)(a))-3))
#define CLASS_ID(a) (*(((jint*)(a))-2))
#define NEW_ARRAY(size, length) new_array(0, size, length) #define NEW_ARRAY(size, length) new_array(0, size, length)
#define NEW_OBJ_ARRAY(length) new_array(1, sizeof(void*), length) #define NEW_OBJ_ARRAY(length) new_array(0, sizeof(void*), length)
#define NEW_OBJ(typeId, typeName) new_object(typeId, sizeof(struct typeName)) #define NEW_OBJ(typeId, typeName) new_object(typeId, sizeof(struct typeName))
#define SET(variable, p) set_object(variable, p) #define SET(variable, p) set_object(variable, p)
#define STRING(s) ((java_lang_String*) string(s)) #define STRING(s) ((java_lang_String*) string(s))
...@@ -42,6 +43,13 @@ void* string(char* s); ...@@ -42,6 +43,13 @@ void* string(char* s);
*/ */
/*
* Object layout:
* m-3: arrays: length; otherwise not allocated
* m-2: arrays: 0; otherwise type
* m-1: number of references
*/
/** /**
* A java.lang.String implementation. * A java.lang.String implementation.
*/ */
...@@ -50,32 +58,36 @@ public class String { ...@@ -50,32 +58,36 @@ public class String {
/* c: /* c:
void* new_array(jint object, jint size, jint length) { void* new_array(jint object, jint size, jint length) {
int count = sizeof(jint) * 2 + size * length; int count = sizeof(jint) * 3 + size * length;
int* m = (jint*) calloc(1, count); int* m = (jint*) calloc(1, count);
*m = (object << 31) + length; *m = length;
*(m+1) = 1; *(m + 2) = 1;
return m + 2; return m + 3;
} }
void* new_object(jint type, jint size) { void* new_object(jint type, jint size) {
int count = sizeof(jint) * 2 + size; int count = sizeof(jint) * 2 + size;
int* m = (jint*) calloc(1, count); int* m = (jint*) calloc(1, count);
*m = type; *m = type;
*(m+1) = 1; *(m + 1) = 1;
return m + 2; return m + 2;
} }
void* set_object(void** target, void* o) { void* set_object(void** target, void* o) {
int* m = (jint*) target; int* m = (jint*) target;
if (*(m - 2) == 1) { if (*(m - 1) == 1) {
free(m - 1); if (*(m - 2) == 0) {
free(m - 3);
} else { } else {
(*(m - 2))--; free(m - 2);
}
} else {
(*(m - 1))--;
} }
*target = o; *target = o;
m = (jint*) target; m = (jint*) target;
if (o != 0) { if (o != 0) {
(*(m - 2))++; (*(m - 1))++;
} }
return m; return m;
} }
...@@ -91,12 +103,16 @@ void* string(char* s) { ...@@ -91,12 +103,16 @@ void* string(char* s) {
*/ */
private char[] chars; /**
* The character array.
*/
char[] chars;
private int hashCode; private int hashCode;
public String(char[] chars) { public String(char[] chars) {
this.chars = new char[chars.length]; this.chars = new char[chars.length];
System.arraycopyChars(chars, 0, this.chars, 0, chars.length); System.arraycopy(chars, 0, this.chars, 0, chars.length);
} }
public int hashCode() { public int hashCode() {
......
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.java.lang;
/**
* A java.lang.String implementation.
*/
public class StringBuilder {
private int length;
private char[] buffer;
public StringBuilder() {
buffer = new char[10];
}
/**
* Append the given string.
*
* @param s the string
* @return this
*/
public StringBuilder append(String s) {
int l = s.length();
ensureCapacity(l);
System.arraycopy(s.chars, 0, buffer, length, l);
length += l;
return this;
}
private void ensureCapacity(int plus) {
if (buffer.length < length + plus) {
char[] b = new char[Math.max(length + plus, buffer.length * 2)];
System.arraycopy(buffer, 0, b, 0, length);
buffer = b;
}
}
}
...@@ -28,7 +28,7 @@ public class System { ...@@ -28,7 +28,7 @@ public class System {
* @param destPos the first element in the destination * @param destPos the first element in the destination
* @param length the number of element to copy * @param length the number of element to copy
*/ */
public static void arraycopyChars(char[] src, int srcPos, char[] dest, int destPos, int length) { public static void arraycopy(char[] src, int srcPos, char[] dest, int destPos, int length) {
/* c: /* c:
memmove(((jchar*)dest) + destPos, memmove(((jchar*)dest) + destPos,
((jchar*)src) + srcPos, sizeof(jchar) * length); ((jchar*)src) + srcPos, sizeof(jchar) * length);
...@@ -45,7 +45,7 @@ public class System { ...@@ -45,7 +45,7 @@ public class System {
* @param destPos the first element in the destination * @param destPos the first element in the destination
* @param length the number of element to copy * @param length the number of element to copy
*/ */
public static void arraycopyByte(byte[] src, int srcPos, byte[] dest, int destPos, int length) { public static void arraycopy(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
/* c: /* c:
memmove(((jbyte*)dest) + destPos, memmove(((jbyte*)dest) + destPos,
((jbyte*)src) + srcPos, sizeof(jbyte) * length); ((jbyte*)src) + srcPos, sizeof(jbyte) * length);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论