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

A Java parser / Java to C converter.

上级 04db103b
......@@ -47,7 +47,7 @@ class CallExpr implements Expr {
MethodObj m = classObj.getMethod(name, args);
String methodName;
if (m.isVirtual) {
methodName = "*virtual_" + m.name + "[CLASS_ID("+expr.toString()+")]";
methodName = "virtual_" + m.name + "[CLASS_ID("+expr.toString()+")]";
} else {
methodName = JavaParser.toC(classObj.toString() + "." + m.name);
}
......@@ -224,7 +224,7 @@ class NewExpr implements Expr {
buff.append(")");
}
} else {
buff.append("NEW_OBJ(" + type.id + ", " + type + ")");
buff.append("NEW_OBJ(" + type.id + ", " + JavaParser.toC(type.toString()) + ")");
}
return buff.toString();
}
......
......@@ -28,10 +28,10 @@ public class JavaParser {
private static final int TOKEN_OTHER = 5;
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 int firstClassId;
private final ArrayList<ClassObj> allClasses = new ArrayList<ClassObj>();
private final HashMap<String, ClassObj> builtInTypes = new HashMap<String, ClassObj>();
private String source;
......@@ -39,7 +39,7 @@ public class JavaParser {
private String packageName;
private ClassObj classObj;
private int classId = firstClassId;
private int nextClassId;
private MethodObj method;
private FieldObj thisPointer;
private HashMap<String, String> importMap = new HashMap<String, String>();
......@@ -49,7 +49,11 @@ public class JavaParser {
private ArrayList<Statement> nativeHeaders = new ArrayList<Statement>();
static {
public JavaParser() {
addBuiltInTypes();
}
private void addBuiltInTypes() {
String[] list = { "abstract", "continue", "for", "new", "switch", "assert", "default", "if",
"package", "synchronized", "boolean", "do", "goto", "private", "this", "break", "double", "implements",
"protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof",
......@@ -76,16 +80,25 @@ public class JavaParser {
JAVA_IMPORT_MAP.put(s, "java.lang." + s);
addBuiltInType(id++, false, 0, "java.lang." + s);
}
firstClassId = id;
nextClassId = id;
}
private static void addBuiltInType(int id, boolean primitive, int primitiveType, String type) {
ClassObj typeObj = new ClassObj();
typeObj.id = id;
typeObj.name = type;
typeObj.isPrimitive = primitive;
typeObj.primitiveType = primitiveType;
BUILT_IN_TYPES.put(type, typeObj);
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 void addClass(ClassObj c) {
int id = c.id;
while (id >= allClasses.size()) {
allClasses.add(null);
}
allClasses.set(id, c);
}
/**
......@@ -151,16 +164,17 @@ public class JavaParser {
isInterface = true;
}
String name = readIdentifier();
classObj = BUILT_IN_TYPES.get(packageName + "." + name);
classObj = builtInTypes.get(packageName + "." + name);
if (classObj == null) {
classObj = new ClassObj();
classObj.id = classId++;
classObj.id = nextClassId++;
}
classObj.isPublic = isPublic;
classObj.isInterface = isInterface;
classObj.name = packageName == null ? "" : (packageName + ".") + name;
// import this class
importMap.put(name, classObj.name);
addClass(classObj);
classes.put(classObj.name, classObj);
if (readIf("extends")) {
classObj.superClassName = readQualifiedIdentifier();
......@@ -181,7 +195,7 @@ public class JavaParser {
}
private boolean isTypeOrIdentifier() {
if (BUILT_IN_TYPES.containsKey(current.token)) {
if (builtInTypes.containsKey(current.token)) {
return true;
}
return current.type == TOKEN_IDENTIFIER;
......@@ -196,7 +210,7 @@ public class JavaParser {
}
private ClassObj getClassIf(String type) {
ClassObj c = BUILT_IN_TYPES.get(type);
ClassObj c = builtInTypes.get(type);
if (c != null) {
return c;
}
......@@ -213,7 +227,7 @@ public class JavaParser {
}
c = classes.get(mappedType);
if (c == null) {
c = BUILT_IN_TYPES.get(mappedType);
c = builtInTypes.get(mappedType);
if (c == null) {
throw new RuntimeException("Unknown class: " + mappedType);
}
......@@ -409,7 +423,7 @@ public class JavaParser {
private String readTypeOrIdentifier() {
if (current.type == TOKEN_RESERVED) {
if (BUILT_IN_TYPES.containsKey(current.token)) {
if (builtInTypes.containsKey(current.token)) {
return read();
}
}
......@@ -745,7 +759,7 @@ public class JavaParser {
expr = e2;
if (n.equals("length") && t.arrayLevel > 0) {
e2.field = new FieldObj();
e2.field.type = BUILT_IN_TYPES.get("int").baseType;
e2.field.type = builtInTypes.get("int").baseType;
e2.field.name = "length";
} else {
if (t == null || t.classObj == null) {
......@@ -1380,11 +1394,11 @@ public class JavaParser {
i++;
}
out.println(") = {");
for (ClassObj c : classes.values()) {
if (c.methods.containsKey(m.name)) {
out.println(" " + toC(c.name) + "_" + m.name + ", ");
for (ClassObj c : allClasses) {
if (c != null && c.methods.containsKey(m.name)) {
out.println(toC(c.name) + "_" + m.name + ", ");
} else {
out.println(" 0, ");
out.print("0, ");
}
}
out.println("};");
......@@ -1494,7 +1508,7 @@ public class JavaParser {
* @return the class
*/
ClassObj getClassObj(String className) {
ClassObj c = BUILT_IN_TYPES.get(className);
ClassObj c = builtInTypes.get(className);
if (c == null) {
c = classes.get(className);
}
......
......@@ -25,8 +25,7 @@ int main(int argc, char** argv) {
* @param args the command line arguments
*/
public static void main(String... args) {
System.out.println("Hello World");
System.out.println("Hello!");
System.out.println("Hello " + "World" + 1);
}
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;
#define false 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_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 SET(variable, p) set_object(variable, p)
#define STRING(s) ((java_lang_String*) string(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.
*/
......@@ -50,32 +58,36 @@ public class String {
/* c:
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);
*m = (object << 31) + length;
*(m+1) = 1;
return m + 2;
*m = length;
*(m + 2) = 1;
return m + 3;
}
void* new_object(jint type, jint size) {
int count = sizeof(jint) * 2 + size;
int* m = (jint*) calloc(1, count);
*m = type;
*(m+1) = 1;
*(m + 1) = 1;
return m + 2;
}
void* set_object(void** target, void* o) {
int* m = (jint*) target;
if (*(m - 2) == 1) {
free(m - 1);
if (*(m - 1) == 1) {
if (*(m - 2) == 0) {
free(m - 3);
} else {
free(m - 2);
}
} else {
(*(m - 2))--;
(*(m - 1))--;
}
*target = o;
m = (jint*) target;
if (o != 0) {
(*(m - 2))++;
(*(m - 1))++;
}
return m;
}
......@@ -91,12 +103,16 @@ void* string(char* s) {
*/
private char[] chars;
/**
* The character array.
*/
char[] chars;
private int hashCode;
public String(char[] chars) {
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() {
......
/*
* 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 {
* @param destPos the first element in the destination
* @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:
memmove(((jchar*)dest) + destPos,
((jchar*)src) + srcPos, sizeof(jchar) * length);
......@@ -45,7 +45,7 @@ public class System {
* @param destPos the first element in the destination
* @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:
memmove(((jbyte*)dest) + destPos,
((jbyte*)src) + srcPos, sizeof(jbyte) * length);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论