提交 42c4f92d authored 作者: Thomas Mueller's avatar Thomas Mueller

Documentation / cleanup

上级 e4842f9d
...@@ -372,6 +372,11 @@ class Type { ...@@ -372,6 +372,11 @@ class Type {
return asString(); return asString();
} }
/**
* Get the C++ code.
*
* @return the C++ code
*/
public String asString() { public String asString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
for (int i = 0; i < arrayLevel; i++) { for (int i = 0; i < arrayLevel; i++) {
...@@ -392,11 +397,15 @@ class Type { ...@@ -392,11 +397,15 @@ class Type {
} }
} }
for (int i = 0; i < arrayLevel; i++) { for (int i = 0; i < arrayLevel; i++) {
buff.append(" >");
if (refCount) { if (refCount) {
buff.append(" >"); buff.append(" >");
} else {
if (!classObj.isPrimitive) {
buff.append("*");
} }
} }
buff.append(" >");
}
if (!refCount) { if (!refCount) {
if (isObject()) { if (isObject()) {
buff.append("*"); buff.append("*");
...@@ -417,5 +426,26 @@ class Type { ...@@ -417,5 +426,26 @@ class Type {
return false; return false;
} }
/**
* Get the default value, for primitive types (0 usually).
*
* @param context the context
* @return the expression
*/
public Expr getDefaultValue(JavaParser context) {
if (classObj.isPrimitive) {
LiteralExpr literal = new LiteralExpr(context, classObj.className);
literal.literal = "0";
CastExpr cast = new CastExpr();
cast.type = this;
cast.expr = literal;
cast.type = this;
return cast;
}
LiteralExpr literal = new LiteralExpr(context, classObj.className);
literal.literal = "null";
return literal;
}
} }
...@@ -14,9 +14,15 @@ import java.util.Iterator; ...@@ -14,9 +14,15 @@ import java.util.Iterator;
*/ */
public interface Expr { public interface Expr {
/**
* Get the C++ code.
*
* @return the C++ code
*/
String asString(); String asString();
Type getType(); Type getType();
Expr cast(Type type); void setType(Type type);
} }
...@@ -34,13 +40,18 @@ abstract class ExprBase implements Expr { ...@@ -34,13 +40,18 @@ abstract class ExprBase implements Expr {
*/ */
class CallExpr extends ExprBase { class CallExpr extends ExprBase {
/**
* The parameters.
*/
final ArrayList<Expr> args = new ArrayList<Expr>(); final ArrayList<Expr> args = new ArrayList<Expr>();
private final JavaParser context; private final JavaParser context;
private final String className; private final String className;
private final String name; private final String name;
private Expr expr; private Expr expr;
private ClassObj classObj; private ClassObj classObj;
private MethodObj method; private MethodObj method;
private Type type;
CallExpr(JavaParser context, Expr expr, String className, String name) { CallExpr(JavaParser context, Expr expr, String className, String name) {
this.context = context; this.context = context;
...@@ -93,7 +104,8 @@ class CallExpr extends ExprBase { ...@@ -93,7 +104,8 @@ class CallExpr extends ExprBase {
} }
FieldObj f = paramIt.next(); FieldObj f = paramIt.next();
i++; i++;
buff.append(a.cast(f.type).asString()); a.setType(f.type);
buff.append(a.asString());
} }
buff.append(")"); buff.append(")");
} }
...@@ -105,8 +117,8 @@ class CallExpr extends ExprBase { ...@@ -105,8 +117,8 @@ class CallExpr extends ExprBase {
return method.returnType; return method.returnType;
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -116,20 +128,37 @@ class CallExpr extends ExprBase { ...@@ -116,20 +128,37 @@ class CallExpr extends ExprBase {
*/ */
class AssignExpr extends ExprBase { class AssignExpr extends ExprBase {
/**
* The target variable or field.
*/
Expr left; Expr left;
/**
* The operation (=, +=,...).
*/
String op; String op;
/**
* The expression.
*/
Expr right; Expr right;
/**
* The type.
*/
Type type;
public String asString() { public String asString() {
return left.asString() + " " + op + " " + right.cast(left.getType()).asString(); right.setType(left.getType());
return left.asString() + " " + op + " " + right.asString();
} }
public Type getType() { public Type getType() {
return left.getType(); return left.getType();
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -139,8 +168,20 @@ class AssignExpr extends ExprBase { ...@@ -139,8 +168,20 @@ class AssignExpr extends ExprBase {
*/ */
class ConditionalExpr extends ExprBase { class ConditionalExpr extends ExprBase {
/**
* The condition.
*/
Expr condition; Expr condition;
Expr ifTrue, ifFalse;
/**
* The 'true' expression.
*/
Expr ifTrue;
/**
* The 'false' expression.
*/
Expr ifFalse;
public String asString() { public String asString() {
return condition.asString() + " ? " + ifTrue.asString() + " : " + ifFalse.asString(); return condition.asString() + " ? " + ifTrue.asString() + " : " + ifFalse.asString();
...@@ -150,12 +191,9 @@ class ConditionalExpr extends ExprBase { ...@@ -150,12 +191,9 @@ class ConditionalExpr extends ExprBase {
return ifTrue.getType(); return ifTrue.getType();
} }
public Expr cast(Type type) { public void setType(Type type) {
ConditionalExpr e2 = new ConditionalExpr(); ifTrue.setType(type);
e2.condition = condition; ifFalse.setType(type);
e2.ifTrue = ifTrue.cast(type);
e2.ifFalse = ifFalse.cast(type);
return e2;
} }
} }
...@@ -165,7 +203,11 @@ class ConditionalExpr extends ExprBase { ...@@ -165,7 +203,11 @@ class ConditionalExpr extends ExprBase {
*/ */
class LiteralExpr extends ExprBase { class LiteralExpr extends ExprBase {
/**
* The literal expression.
*/
String literal; String literal;
private final JavaParser context; private final JavaParser context;
private final String className; private final String className;
private Type type; private Type type;
...@@ -177,7 +219,11 @@ class LiteralExpr extends ExprBase { ...@@ -177,7 +219,11 @@ class LiteralExpr extends ExprBase {
public String asString() { public String asString() {
if ("null".equals(literal)) { if ("null".equals(literal)) {
return type.asString() + "()"; Type t = getType();
if (t.isObject()) {
return "(" + getType().asString() + ") 0";
}
return t.asString() + "()";
} }
return literal; return literal;
} }
...@@ -190,13 +236,9 @@ class LiteralExpr extends ExprBase { ...@@ -190,13 +236,9 @@ class LiteralExpr extends ExprBase {
return type; return type;
} }
public Expr cast(Type type) { public void setType(Type type) {
if ("null".equals(literal)) {
// TODO should be immutable
this.type = type; this.type = type;
} }
return this;
}
} }
...@@ -205,10 +247,23 @@ class LiteralExpr extends ExprBase { ...@@ -205,10 +247,23 @@ class LiteralExpr extends ExprBase {
*/ */
class OpExpr extends ExprBase { class OpExpr extends ExprBase {
/**
* The left hand side.
*/
Expr left; Expr left;
/**
* The operation.
*/
String op; String op;
/**
* The right hand side.
*/
Expr right; Expr right;
private final JavaParser context; private final JavaParser context;
private Type type;
OpExpr(JavaParser context) { OpExpr(JavaParser context) {
this.context = context; this.context = context;
...@@ -227,7 +282,7 @@ class OpExpr extends ExprBase { ...@@ -227,7 +282,7 @@ class OpExpr extends ExprBase {
if (left.getType().isObject() || right.getType().isObject()) { if (left.getType().isObject() || right.getType().isObject()) {
// TODO convert primitive to to String, call toString // TODO convert primitive to to String, call toString
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
if (JavaParser.REF_COUNT) { if (type.refCount) {
buff.append("ptr<java_lang_StringBuilder>(new java_lang_StringBuilder("); buff.append("ptr<java_lang_StringBuilder>(new java_lang_StringBuilder(");
} else { } else {
buff.append("(new java_lang_StringBuilder("); buff.append("(new java_lang_StringBuilder(");
...@@ -288,8 +343,8 @@ class OpExpr extends ExprBase { ...@@ -288,8 +343,8 @@ class OpExpr extends ExprBase {
return lt; return lt;
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -299,31 +354,55 @@ class OpExpr extends ExprBase { ...@@ -299,31 +354,55 @@ class OpExpr extends ExprBase {
*/ */
class NewExpr extends ExprBase { class NewExpr extends ExprBase {
/**
* The class.
*/
ClassObj classObj; ClassObj classObj;
ArrayList<Expr> arrayInitExpr = new ArrayList<Expr>();
ArrayList<Expr> args = new ArrayList<Expr>();
final JavaParser context;
NewExpr(JavaParser context) { /**
this.context = context; * The constructor parameters (for objects).
} */
final ArrayList<Expr> args = new ArrayList<Expr>();
/**
* The array bounds (for arrays).
*/
final ArrayList<Expr> arrayInitExpr = new ArrayList<Expr>();
/**
* The type.
*/
Type type;
public String asString() { public String asString() {
boolean refCount = type.refCount;
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
if (arrayInitExpr.size() > 0) { if (arrayInitExpr.size() > 0) {
if (JavaParser.REF_COUNT) { if (refCount) {
buff.append("ptr< array< " + classObj.toString() + " > >"); if (classObj.isPrimitive) {
buff.append("ptr< array< " + classObj + " > >");
} else {
buff.append("ptr< array< ptr< " + classObj + " > > >");
}
} }
if (classObj.isPrimitive) {
buff.append("(new array< " + classObj + " >(1 "); buff.append("(new array< " + classObj + " >(1 ");
} else {
if (refCount) {
buff.append("(new array< ptr< " + classObj + " > >(1 ");
} else {
buff.append("(new array< " + classObj + "* >(1 ");
}
}
for (Expr e : arrayInitExpr) { for (Expr e : arrayInitExpr) {
buff.append("* ").append(e.asString()); buff.append("* ").append(e.asString());
} }
buff.append("))"); buff.append("))");
} else { } else {
if (JavaParser.REF_COUNT) { if (refCount) {
buff.append("ptr< " + classObj.toString() + " >"); buff.append("ptr< " + classObj + " >");
} }
buff.append("(new " + JavaParser.toC(classObj.toString())); buff.append("(new " + classObj);
buff.append("("); buff.append("(");
int i = 0; int i = 0;
for (Expr a : args) { for (Expr a : args) {
...@@ -344,8 +423,8 @@ class NewExpr extends ExprBase { ...@@ -344,8 +423,8 @@ class NewExpr extends ExprBase {
return t; return t;
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -438,8 +517,7 @@ class StringExpr extends ExprBase { ...@@ -438,8 +517,7 @@ class StringExpr extends ExprBase {
return buff.toString(); return buff.toString();
} }
public Expr cast(Type type) { public void setType(Type type) {
return this;
} }
} }
...@@ -449,9 +527,22 @@ class StringExpr extends ExprBase { ...@@ -449,9 +527,22 @@ class StringExpr extends ExprBase {
*/ */
class VariableExpr extends ExprBase { class VariableExpr extends ExprBase {
/**
* The variable name.
*/
String name;
/**
* The base expression (the first element in a.b variables).
*/
Expr base; Expr base;
/**
* The field.
*/
FieldObj field; FieldObj field;
String name;
private Type type;
private final JavaParser context; private final JavaParser context;
VariableExpr(JavaParser context) { VariableExpr(JavaParser context) {
...@@ -499,35 +590,8 @@ class VariableExpr extends ExprBase { ...@@ -499,35 +590,8 @@ class VariableExpr extends ExprBase {
return field.type; return field.type;
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
}
}
/**
* A array access expression.
*/
class ArrayExpr extends ExprBase {
Expr expr;
ArrayList<Expr> indexes = new ArrayList<Expr>();
public String asString() {
StringBuilder buff = new StringBuilder();
buff.append(expr.toString());
for (Expr e : indexes) {
buff.append('[').append(e.toString()).append(']');
}
return buff.toString();
}
public Type getType() {
return expr.getType();
}
public Expr cast(Type type) {
return this;
} }
} }
...@@ -537,8 +601,15 @@ class ArrayExpr extends ExprBase { ...@@ -537,8 +601,15 @@ class ArrayExpr extends ExprBase {
*/ */
class ArrayInitExpr extends ExprBase { class ArrayInitExpr extends ExprBase {
/**
* The expression list.
*/
final ArrayList<Expr> list = new ArrayList<Expr>();
/**
* The type.
*/
Type type; Type type;
ArrayList<Expr> list = new ArrayList<Expr>();
public Type getType() { public Type getType() {
return type; return type;
...@@ -557,8 +628,8 @@ class ArrayInitExpr extends ExprBase { ...@@ -557,8 +628,8 @@ class ArrayInitExpr extends ExprBase {
return buff.toString(); return buff.toString();
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -568,9 +639,16 @@ class ArrayInitExpr extends ExprBase { ...@@ -568,9 +639,16 @@ class ArrayInitExpr extends ExprBase {
*/ */
class CastExpr extends ExprBase { class CastExpr extends ExprBase {
Type type; /**
* The expression.
*/
Expr expr; Expr expr;
/**
* The cast type.
*/
Type type;
public Type getType() { public Type getType() {
return type; return type;
} }
...@@ -579,8 +657,8 @@ class CastExpr extends ExprBase { ...@@ -579,8 +657,8 @@ class CastExpr extends ExprBase {
return "(" + type.asString() + ") " + expr.asString(); return "(" + type.asString() + ") " + expr.asString();
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -590,9 +668,21 @@ class CastExpr extends ExprBase { ...@@ -590,9 +668,21 @@ class CastExpr extends ExprBase {
*/ */
class ArrayAccessExpr extends ExprBase { class ArrayAccessExpr extends ExprBase {
/**
* The base expression.
*/
Expr base; Expr base;
/**
* The index.
*/
Expr index; Expr index;
/**
* The type.
*/
Type type;
public Type getType() { public Type getType() {
Type t = new Type(); Type t = new Type();
t.classObj = base.getType().classObj; t.classObj = base.getType().classObj;
...@@ -604,8 +694,8 @@ class ArrayAccessExpr extends ExprBase { ...@@ -604,8 +694,8 @@ class ArrayAccessExpr extends ExprBase {
return base.asString() + "->at(" + index.asString() + ")"; return base.asString() + "->at(" + index.asString() + ")";
} }
public Expr cast(Type type) { public void setType(Type type) {
return this; this.type = type;
} }
} }
...@@ -22,7 +22,15 @@ import org.h2.util.New; ...@@ -22,7 +22,15 @@ import org.h2.util.New;
*/ */
public class JavaParser { public class JavaParser {
public static final boolean REF_COUNT = true; /**
* Whether ref-counting is used.
*/
public static final boolean REF_COUNT = false;
/**
* Whether ref-counting is used for constants.
*/
public static final boolean REF_COUNT_STATIC = false;
private static final HashMap<String, ClassObj> BUILT_IN_CLASSES = New.hashMap(); private static final HashMap<String, ClassObj> BUILT_IN_CLASSES = New.hashMap();
...@@ -245,6 +253,12 @@ public class JavaParser { ...@@ -245,6 +253,12 @@ public class JavaParser {
return c; return c;
} }
/**
* Get the class for a built-in type.
*
* @param type the type
* @return the class or null if not found
*/
static ClassObj getBuiltInClass(String type) { static ClassObj getBuiltInClass(String type) {
return BUILT_IN_CLASSES.get(type); return BUILT_IN_CLASSES.get(type);
} }
...@@ -386,6 +400,8 @@ public class JavaParser { ...@@ -386,6 +400,8 @@ public class JavaParser {
} else { } else {
field.value = readExpr(); field.value = readExpr();
} }
} else {
field.value = field.type.getDefaultValue(this);
} }
read(";"); read(";");
if (isStatic) { if (isStatic) {
...@@ -495,8 +511,8 @@ public class JavaParser { ...@@ -495,8 +511,8 @@ public class JavaParser {
while (source.charAt(current.index) != '\n') { while (source.charAt(current.index) != '\n') {
current.index++; current.index++;
} }
StatementNative stat = new StatementNative(); String s = source.substring(start, current.index).trim();
stat.code = source.substring(start, current.index).trim(); StatementNative stat = new StatementNative(s);
read(); read();
return isC ? stat : null; return isC ? stat : null;
} else if (readIf("/*")) { } else if (readIf("/*")) {
...@@ -505,8 +521,8 @@ public class JavaParser { ...@@ -505,8 +521,8 @@ public class JavaParser {
while (source.charAt(current.index) != '*' || source.charAt(current.index + 1) != '/') { while (source.charAt(current.index) != '*' || source.charAt(current.index + 1) != '/') {
current.index++; current.index++;
} }
StatementNative stat = new StatementNative(); String s = source.substring(start, current.index).trim();
stat.code = source.substring(start, current.index).trim(); StatementNative stat = new StatementNative(s);
current.index += 2; current.index += 2;
read(); read();
return isC ? stat : null; return isC ? stat : null;
...@@ -554,16 +570,16 @@ public class JavaParser { ...@@ -554,16 +570,16 @@ public class JavaParser {
read(";"); read(";");
return new ContinueStatement(); return new ContinueStatement();
} else if (readIf("switch")) { } else if (readIf("switch")) {
SwitchStatement switchStat = new SwitchStatement();
read("("); read("(");
switchStat.expr = readExpr(); SwitchStatement switchStat = new SwitchStatement(readExpr());
read(")"); read(")");
read("{"); read("{");
while (true) { while (true) {
if (readIf("default")) { if (readIf("default")) {
read(":"); read(":");
StatementBlock block = new StatementBlock(); StatementBlock block = new StatementBlock();
switchStat.defaultBlock = block; switchStat.setDefaultBlock(block);
while (true) { while (true) {
block.instructions.add(readStatement()); block.instructions.add(readStatement());
if (current.token.equals("case") || current.token.equals("default") || current.token.equals("}")) { if (current.token.equals("case") || current.token.equals("default") || current.token.equals("}")) {
...@@ -571,7 +587,7 @@ public class JavaParser { ...@@ -571,7 +587,7 @@ public class JavaParser {
} }
} }
} else if (readIf("case")) { } else if (readIf("case")) {
switchStat.cases.add(readExpr()); Expr expr = readExpr();
read(":"); read(":");
StatementBlock block = new StatementBlock(); StatementBlock block = new StatementBlock();
while (true) { while (true) {
...@@ -580,7 +596,7 @@ public class JavaParser { ...@@ -580,7 +596,7 @@ public class JavaParser {
break; break;
} }
} }
switchStat.blocks.add(block); switchStat.addCase(expr, block);
} else if (readIf("}")) { } else if (readIf("}")) {
break; break;
} }
...@@ -654,8 +670,7 @@ public class JavaParser { ...@@ -654,8 +670,7 @@ public class JavaParser {
f.type = dec.type; f.type = dec.type;
f.name = varName; f.name = varName;
localVars.put(varName, f); localVars.put(varName, f);
dec.variables.add(varName); dec.addVariable(varName, value);
dec.values.add(value);
if (readIf(";")) { if (readIf(";")) {
break; break;
} }
...@@ -666,8 +681,7 @@ public class JavaParser { ...@@ -666,8 +681,7 @@ public class JavaParser {
current = start; current = start;
// ExprStatement // ExprStatement
} }
ExprStatement stat = new ExprStatement(); ExprStatement stat = new ExprStatement(readExpr());
stat.expr = readExpr();
read(";"); read(";");
return stat; return stat;
} }
...@@ -992,7 +1006,7 @@ public class JavaParser { ...@@ -992,7 +1006,7 @@ public class JavaParser {
if (ch >= 'a' && ch <= 'z') { if (ch >= 'a' && ch <= 'z') {
// don't use Character.toUpperCase // don't use Character.toUpperCase
// to avoid locale problems // to avoid locale problems
// (the uppercase of i isn't always I) // (the uppercase of 'i' is not always 'I')
buff.append((char) (ch + 'A' - 'a')); buff.append((char) (ch + 'A' - 'a'));
} else if (ch >= 'A' && ch <= 'Z') { } else if (ch >= 'A' && ch <= 'Z') {
buff.append(ch); buff.append(ch);
...@@ -1026,7 +1040,7 @@ public class JavaParser { ...@@ -1026,7 +1040,7 @@ public class JavaParser {
private Expr readExpr5() { private Expr readExpr5() {
if (readIf("new")) { if (readIf("new")) {
NewExpr expr = new NewExpr(this); NewExpr expr = new NewExpr();
String typeName = readTypeOrIdentifier(); String typeName = readTypeOrIdentifier();
expr.classObj = getClass(typeName); expr.classObj = getClass(typeName);
if (readIf("(")) { if (readIf("(")) {
...@@ -1550,7 +1564,12 @@ public class JavaParser { ...@@ -1550,7 +1564,12 @@ public class JavaParser {
*/ */
void writeHeader(PrintWriter out) { void writeHeader(PrintWriter out) {
for (Statement s : nativeHeaders) { for (Statement s : nativeHeaders) {
out.println(s); out.println(s.asString());
}
if (JavaParser.REF_COUNT_STATIC) {
out.println("#define STRING(s) STRING_REF(s)");
} else {
out.println("#define STRING(s) STRING_PTR(s)");
} }
out.println(); out.println();
for (ClassObj c : classes.values()) { for (ClassObj c : classes.values()) {
...@@ -1603,9 +1622,6 @@ public class JavaParser { ...@@ -1603,9 +1622,6 @@ public class JavaParser {
for (FieldObj f : c.instanceFields.values()) { for (FieldObj f : c.instanceFields.values()) {
out.print(" "); out.print(" ");
out.print(f.type.asString() + " " + f.name); out.print(f.type.asString() + " " + f.name);
if (f.value != null) {
out.print(" = " + f.value);
}
out.println(";"); out.println(";");
} }
out.println("public:"); out.println("public:");
...@@ -1640,7 +1656,7 @@ public class JavaParser { ...@@ -1640,7 +1656,7 @@ public class JavaParser {
Collections.sort(constantNames); Collections.sort(constantNames);
for (String c : constantNames) { for (String c : constantNames) {
String s = stringConstantToStringMap.get(c); String s = stringConstantToStringMap.get(c);
if (JavaParser.REF_COUNT) { if (JavaParser.REF_COUNT_STATIC) {
out.println("ptr<java_lang_String> " + c + " = STRING(L\"" + s + "\");"); out.println("ptr<java_lang_String> " + c + " = STRING(L\"" + s + "\");");
} else { } else {
out.println("java_lang_String* " + c + " = STRING(L\"" + s + "\");"); out.println("java_lang_String* " + c + " = STRING(L\"" + s + "\");");
...@@ -1655,9 +1671,9 @@ public class JavaParser { ...@@ -1655,9 +1671,9 @@ public class JavaParser {
*/ */
void writeSource(PrintWriter out) { void writeSource(PrintWriter out) {
for (ClassObj c : classes.values()) { for (ClassObj c : classes.values()) {
out.println("/* " + c.className + ".cpp */"); out.println("/* " + c.className + " */");
for (Statement s : c.nativeCode) { for (Statement s : c.nativeCode) {
out.println(s); out.println(s.asString());
} }
for (FieldObj f : c.staticFields.values()) { for (FieldObj f : c.staticFields.values()) {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
...@@ -1694,10 +1710,16 @@ public class JavaParser { ...@@ -1694,10 +1710,16 @@ public class JavaParser {
} }
out.println(") {"); out.println(") {");
if (m.isConstructor) { if (m.isConstructor) {
// TODO for (FieldObj f : c.instanceFields.values()) {
out.print(" ");
out.print("this->" + f.name);
out.print(" = " + f.value.asString());
out.println(";");
}
} }
if (m.block != null) { if (m.block != null) {
out.print(m.block.toString()); m.block.setMethod(m);
out.print(m.block.asString());
} }
out.println("}"); out.println("}");
out.println(); out.println();
......
...@@ -13,9 +13,15 @@ import java.util.ArrayList; ...@@ -13,9 +13,15 @@ import java.util.ArrayList;
*/ */
public interface Statement { public interface Statement {
void setMethod(MethodObj method);
boolean isEnd(); boolean isEnd();
// toString /**
* Get the C++ code.
*
* @return the C++ code
*/
String asString();
} }
...@@ -35,16 +41,27 @@ abstract class StatementBase implements Statement { ...@@ -35,16 +41,27 @@ abstract class StatementBase implements Statement {
*/ */
class ReturnStatement extends StatementBase { class ReturnStatement extends StatementBase {
/**
* The return expression.
*/
Expr expr; Expr expr;
public String toString() { private MethodObj method;
public void setMethod(MethodObj method) {
this.method = method;
}
public String asString() {
if (expr == null) { if (expr == null) {
return "return;"; return "return;";
} }
Type returnType = method.returnType;
expr.setType(returnType);
if (!expr.getType().isObject()) { if (!expr.getType().isObject()) {
return "return " + expr.asString() + ";"; return "return " + expr.asString() + ";";
} }
if (JavaParser.REF_COUNT) { if (returnType.refCount) {
return "return " + expr.getType().asString() + "(" + expr.asString() + ");"; return "return " + expr.getType().asString() + "(" + expr.asString() + ");";
} }
return "return " + expr.asString() + ";"; return "return " + expr.asString() + ";";
...@@ -57,10 +74,21 @@ class ReturnStatement extends StatementBase { ...@@ -57,10 +74,21 @@ class ReturnStatement extends StatementBase {
*/ */
class DoWhileStatement extends StatementBase { class DoWhileStatement extends StatementBase {
/**
* The condition.
*/
Expr condition; Expr condition;
/**
* The execution block.
*/
Statement block; Statement block;
public String toString() { public void setMethod(MethodObj method) {
block.setMethod(method);
}
public String asString() {
return "do {\n" + block + "} while (" + condition.asString() + ");"; return "do {\n" + block + "} while (" + condition.asString() + ");";
} }
...@@ -71,7 +99,11 @@ class DoWhileStatement extends StatementBase { ...@@ -71,7 +99,11 @@ class DoWhileStatement extends StatementBase {
*/ */
class ContinueStatement extends StatementBase { class ContinueStatement extends StatementBase {
public String toString() { public void setMethod(MethodObj method) {
// ignore
}
public String asString() {
return "continue;"; return "continue;";
} }
...@@ -82,7 +114,11 @@ class ContinueStatement extends StatementBase { ...@@ -82,7 +114,11 @@ class ContinueStatement extends StatementBase {
*/ */
class BreakStatement extends StatementBase { class BreakStatement extends StatementBase {
public String toString() { public void setMethod(MethodObj method) {
// ignore
}
public String asString() {
return "break;"; return "break;";
} }
...@@ -93,7 +129,11 @@ class BreakStatement extends StatementBase { ...@@ -93,7 +129,11 @@ class BreakStatement extends StatementBase {
*/ */
class EmptyStatement extends StatementBase { class EmptyStatement extends StatementBase {
public String toString() { public void setMethod(MethodObj method) {
// ignore
}
public String asString() {
return ";"; return ";";
} }
...@@ -104,12 +144,23 @@ class EmptyStatement extends StatementBase { ...@@ -104,12 +144,23 @@ class EmptyStatement extends StatementBase {
*/ */
class SwitchStatement extends StatementBase { class SwitchStatement extends StatementBase {
Expr expr; private StatementBlock defaultBlock;
StatementBlock defaultBlock; private final ArrayList<Expr> cases = new ArrayList<Expr>();
ArrayList<Expr> cases = new ArrayList<Expr>(); private final ArrayList<StatementBlock> blocks = new ArrayList<StatementBlock>();
ArrayList<StatementBlock> blocks = new ArrayList<StatementBlock>(); private final Expr expr;
public SwitchStatement(Expr expr) {
this.expr = expr;
}
public void setMethod(MethodObj method) {
defaultBlock.setMethod(method);
for (StatementBlock b : blocks) {
b.setMethod(method);
}
}
public String toString() { public String asString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append("switch (").append(expr.asString()).append(") {\n"); buff.append("switch (").append(expr.asString()).append(") {\n");
for (int i = 0; i < cases.size(); i++) { for (int i = 0; i < cases.size(); i++) {
...@@ -124,6 +175,21 @@ class SwitchStatement extends StatementBase { ...@@ -124,6 +175,21 @@ class SwitchStatement extends StatementBase {
return buff.toString(); return buff.toString();
} }
public void setDefaultBlock(StatementBlock block) {
this.defaultBlock = block;
}
/**
* Add a case.
*
* @param expr the case expression
* @param block the execution block
*/
public void addCase(Expr expr, StatementBlock block) {
cases.add(expr);
blocks.add(block);
}
} }
/** /**
...@@ -131,9 +197,17 @@ class SwitchStatement extends StatementBase { ...@@ -131,9 +197,17 @@ class SwitchStatement extends StatementBase {
*/ */
class ExprStatement extends StatementBase { class ExprStatement extends StatementBase {
Expr expr; private final Expr expr;
public ExprStatement(Expr expr) {
this.expr = expr;
}
public String toString() { public void setMethod(MethodObj method) {
// ignore
}
public String asString() {
return expr.asString() + ";"; return expr.asString() + ";";
} }
...@@ -144,10 +218,21 @@ class ExprStatement extends StatementBase { ...@@ -144,10 +218,21 @@ class ExprStatement extends StatementBase {
*/ */
class WhileStatement extends StatementBase { class WhileStatement extends StatementBase {
/**
* The condition.
*/
Expr condition; Expr condition;
/**
* The execution block.
*/
Statement block; Statement block;
public String toString() { public void setMethod(MethodObj method) {
block.setMethod(method);
}
public String asString() {
String w = "while (" + condition.asString() + ")"; String w = "while (" + condition.asString() + ")";
String s = block.toString(); String s = block.toString();
return w + "\n" + s; return w + "\n" + s;
...@@ -160,15 +245,33 @@ class WhileStatement extends StatementBase { ...@@ -160,15 +245,33 @@ class WhileStatement extends StatementBase {
*/ */
class IfStatement extends StatementBase { class IfStatement extends StatementBase {
/**
* The condition.
*/
Expr condition; Expr condition;
/**
* The execution block.
*/
Statement block; Statement block;
/**
* The else block.
*/
Statement elseBlock; Statement elseBlock;
public String toString() { public void setMethod(MethodObj method) {
block.setMethod(method);
if (elseBlock != null) {
elseBlock.setMethod(method);
}
}
public String asString() {
String w = "if (" + condition.asString() + ") {\n"; String w = "if (" + condition.asString() + ") {\n";
String s = block.toString(); String s = block.asString();
if (elseBlock != null) { if (elseBlock != null) {
s += "} else {\n" + elseBlock.toString(); s += "} else {\n" + elseBlock.asString();
} }
return w + s + "}"; return w + s + "}";
} }
...@@ -180,24 +283,59 @@ class IfStatement extends StatementBase { ...@@ -180,24 +283,59 @@ class IfStatement extends StatementBase {
*/ */
class ForStatement extends StatementBase { class ForStatement extends StatementBase {
/**
* The init block.
*/
Statement init; Statement init;
/**
* The condition.
*/
Expr condition; Expr condition;
/**
* The main loop block.
*/
Statement block; Statement block;
/**
* The update list.
*/
ArrayList<Expr> updates = new ArrayList<Expr>(); ArrayList<Expr> updates = new ArrayList<Expr>();
/**
* The type of the iterable.
*/
Type iterableType; Type iterableType;
/**
* The iterable variable name.
*/
String iterableVariable; String iterableVariable;
/**
* The iterable expression.
*/
Expr iterable; Expr iterable;
public String toString() { public void setMethod(MethodObj method) {
block.setMethod(method);
}
public String asString() {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
buff.append("for ("); buff.append("for (");
if (iterableType != null) { if (iterableType != null) {
Type it = iterable.getType(); Type it = iterable.getType();
if (it != null && it.arrayLevel > 0) { if (it != null && it.arrayLevel > 0) {
String idx = "i_" + iterableVariable; String idx = "i_" + iterableVariable;
buff.append("int " + idx + " = 0; " + idx + " < " + iterable.asString() + "->length(); " + idx + "++"); buff.append("int " + idx + " = 0; " +
idx + " < " + iterable.asString() + "->length(); " +
idx + "++");
buff.append(") {\n"); buff.append(") {\n");
buff.append(JavaParser.indent(iterableType + " " + iterableVariable + " = " + iterable.asString() + "->at("+ idx +");\n")); buff.append(JavaParser.indent(iterableType +
" " + iterableVariable + " = " +
iterable.asString() + "->at("+ idx +");\n"));
buff.append(block.toString()).append("}"); buff.append(block.toString()).append("}");
} else { } else {
// TODO iterate over a collection // TODO iterate over a collection
...@@ -208,7 +346,7 @@ class ForStatement extends StatementBase { ...@@ -208,7 +346,7 @@ class ForStatement extends StatementBase {
buff.append(block.toString()).append("}"); buff.append(block.toString()).append("}");
} }
} else { } else {
buff.append(init.toString()); buff.append(init.asString());
buff.append(" ").append(condition.asString()).append("; "); buff.append(" ").append(condition.asString()).append("; ");
for (int i = 0; i < updates.size(); i++) { for (int i = 0; i < updates.size(); i++) {
if (i > 0) { if (i > 0) {
...@@ -217,7 +355,7 @@ class ForStatement extends StatementBase { ...@@ -217,7 +355,7 @@ class ForStatement extends StatementBase {
buff.append(updates.get(i).asString()); buff.append(updates.get(i).asString());
} }
buff.append(") {\n"); buff.append(") {\n");
buff.append(block.toString()).append("}"); buff.append(block.asString()).append("}");
} }
return buff.toString(); return buff.toString();
} }
...@@ -229,15 +367,24 @@ class ForStatement extends StatementBase { ...@@ -229,15 +367,24 @@ class ForStatement extends StatementBase {
*/ */
class StatementBlock extends StatementBase { class StatementBlock extends StatementBase {
ArrayList<Statement> instructions = new ArrayList<Statement>(); /**
* The list of instructions.
*/
final ArrayList<Statement> instructions = new ArrayList<Statement>();
public void setMethod(MethodObj method) {
for (Statement s : instructions) {
s.setMethod(method);
}
}
public String toString() { public String asString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
for (Statement s : instructions) { for (Statement s : instructions) {
if (s.isEnd()) { if (s.isEnd()) {
break; break;
} }
buff.append(JavaParser.indent(s.toString())); buff.append(JavaParser.indent(s.asString()));
} }
return buff.toString(); return buff.toString();
} }
...@@ -249,11 +396,19 @@ class StatementBlock extends StatementBase { ...@@ -249,11 +396,19 @@ class StatementBlock extends StatementBase {
*/ */
class VarDecStatement extends StatementBase { class VarDecStatement extends StatementBase {
/**
* The type.
*/
Type type; Type type;
ArrayList<String> variables = new ArrayList<String>();
ArrayList<Expr> values = new ArrayList<Expr>();
public String toString() { private ArrayList<String> variables = new ArrayList<String>();
private ArrayList<Expr> values = new ArrayList<Expr>();
public void setMethod(MethodObj method) {
// ignore
}
public String asString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append(type.asString()).append(' '); buff.append(type.asString()).append(' ');
StringBuilder assign = new StringBuilder(); StringBuilder assign = new StringBuilder();
...@@ -268,6 +423,7 @@ class VarDecStatement extends StatementBase { ...@@ -268,6 +423,7 @@ class VarDecStatement extends StatementBase {
if (!value.getType().isObject()) { if (!value.getType().isObject()) {
buff.append(" = ").append(value.asString()); buff.append(" = ").append(value.asString());
} else { } else {
value.setType(type);
assign.append(varName).append(" = ").append(value.asString()).append(";\n"); assign.append(varName).append(" = ").append(value.asString()).append(";\n");
} }
} }
...@@ -280,6 +436,17 @@ class VarDecStatement extends StatementBase { ...@@ -280,6 +436,17 @@ class VarDecStatement extends StatementBase {
return buff.toString(); return buff.toString();
} }
/**
* Add a variable.
*
* @param name the variable name
* @param value the init value
*/
public void addVariable(String name, Expr value) {
variables.add(name);
values.add(value);
}
} }
/** /**
...@@ -287,9 +454,17 @@ class VarDecStatement extends StatementBase { ...@@ -287,9 +454,17 @@ class VarDecStatement extends StatementBase {
*/ */
class StatementNative extends StatementBase { class StatementNative extends StatementBase {
String code; private final String code;
StatementNative(String code) {
this.code = code;
}
public void setMethod(MethodObj method) {
// ignore
}
public String toString() { public String asString() {
return code; return code;
} }
......
...@@ -30,11 +30,12 @@ public class Test extends TestBase { ...@@ -30,11 +30,12 @@ public class Test extends TestBase {
// chmod +x test // chmod +x test
// ./test // ./test
// TODO initialize fields
// include files: // include files:
// /usr/include/c++/4.2.1/tr1/stdio.h // /usr/include/c++/4.2.1/tr1/stdio.h
// /usr/include/stdio.h // /usr/include/stdio.h
// inttypes.h
// TODO initialize fields
// not supported yet: // not supported yet:
// exceptions // exceptions
......
...@@ -26,8 +26,33 @@ int main(int argc, char** argv) { ...@@ -26,8 +26,33 @@ 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) {
for (int i = 0; i < 10; i++) { String[] list = new String[1000];
System.out.println("Hello " + i); for (int i = 0; i < 1000; i++) {
list[i] = "Hello " + i;
}
// time:29244000 mac g++ -O3 without array bound checks
// time:30673000 mac java
// time:32449000 mac g++ -O3
// time:69692000 mac g++ -O3 ref counted
// time:1200000000 raspberry g++ -O3
// time:1720000000 raspberry g++ -O3 ref counted
// time:1980469000 raspberry java IcedTea6 1.8.13 Cacao VM
// time:12962645810 raspberry java IcedTea6 1.8.13 Zero VM
// java -XXaltjvm=cacao
for (int k = 0; k < 4; k++) {
long t = System.nanoTime();
long h = 0;
for (int j = 0; j < 10000; j++) {
for (int i = 0; i < 1000; i++) {
String s = list[i];
h = (h * 7) ^ s.hashCode();
}
}
System.out.println("hash: " + h);
t = System.nanoTime() - t;
System.out.println("time:" + t);
} }
} }
......
...@@ -18,7 +18,7 @@ public class PrintStream { ...@@ -18,7 +18,7 @@ public class PrintStream {
*/ */
public void println(String s) { public void println(String s) {
// c: int x = s->chars->length(); // c: int x = s->chars->length();
// c: printf("%.*S\n", x, s->chars->getData()); // c: printf("%.*S\n", x, s->chars->getPointer());
} }
} }
...@@ -11,8 +11,14 @@ package org.h2.java.lang; ...@@ -11,8 +11,14 @@ package org.h2.java.lang;
*/ */
public class Integer { public class Integer {
/**
* The smallest possible value.
*/
public static final int MIN_VALUE = 1 << 31; public static final int MIN_VALUE = 1 << 31;
/**
* The largest possible value.
*/
public static final int MAX_VALUE = (int) ((1L << 31) - 1); public static final int MAX_VALUE = (int) ((1L << 31) - 1);
/** /**
...@@ -23,7 +29,7 @@ public class Integer { ...@@ -23,7 +29,7 @@ public class Integer {
*/ */
public static String toString(int x) { public static String toString(int x) {
// c: wchar_t ch[20]; // c: wchar_t ch[20];
// c: swprintf(ch, 20, L"%d", x); // c: swprintf(ch, 20, L"%" PRId32, x);
// c: return STRING(ch); // c: return STRING(ch);
// c: return; // c: return;
if (x == MIN_VALUE) { if (x == MIN_VALUE) {
......
...@@ -12,8 +12,14 @@ package org.h2.java.lang; ...@@ -12,8 +12,14 @@ package org.h2.java.lang;
*/ */
public class Long { public class Long {
/**
* The smallest possible value.
*/
public static final long MIN_VALUE = 1L << 63; public static final long MIN_VALUE = 1L << 63;
/**
* The largest possible value.
*/
public static final long MAX_VALUE = (1L << 63) - 1; public static final long MAX_VALUE = (1L << 63) - 1;
/** /**
...@@ -24,7 +30,7 @@ public class Long { ...@@ -24,7 +30,7 @@ public class Long {
*/ */
public static String toString(long x) { public static String toString(long x) {
// c: wchar_t ch[30]; // c: wchar_t ch[30];
// c: swprintf(ch, 30, L"%ld", x); // c: swprintf(ch, 30, L"%" PRId64, x);
// c: return STRING(ch); // c: return STRING(ch);
// c: return; // c: return;
if (x == MIN_VALUE) { if (x == MIN_VALUE) {
......
...@@ -14,8 +14,11 @@ import org.h2.java.Local; ...@@ -14,8 +14,11 @@ import org.h2.java.Local;
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <wchar.h> #include <wchar.h>
#include <stdint.h> #include <stdint.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#define jvoid void #define jvoid void
#define jboolean int8_t #define jboolean int8_t
...@@ -31,9 +34,12 @@ import org.h2.java.Local; ...@@ -31,9 +34,12 @@ import org.h2.java.Local;
#define false 0 #define false 0
#define null 0 #define null 0
#define STRING(s) ptr<java_lang_String>(new java_lang_String(ptr< array<jchar> >(new array<jchar>(s, (jint) wcslen(s))))); #define STRING_REF(s) ptr<java_lang_String> \
(new java_lang_String(ptr< array<jchar> > \
(new array<jchar>(s, (jint) wcslen(s)))));
// #define STRING(s) new java_lang_String(new array<jchar>(s, (jint) wcslen(s))); #define STRING_PTR(s) new java_lang_String \
(new array<jchar>(s, (jint) wcslen(s)));
class RefBase { class RefBase {
protected: protected:
...@@ -86,6 +92,9 @@ public: ...@@ -86,6 +92,9 @@ public:
T& operator*() { T& operator*() {
return *pointer; return *pointer;
} }
T* getPointer() {
return pointer;
}
T* operator->() { T* operator->() {
return pointer; return pointer;
} }
...@@ -112,7 +121,7 @@ public: ...@@ -112,7 +121,7 @@ public:
~array() { ~array() {
delete[] data; delete[] data;
} }
T* getData() { T* getPointer() {
return data; return data;
} }
jint length() { jint length() {
...@@ -180,15 +189,31 @@ public class String { ...@@ -180,15 +189,31 @@ public class String {
return chars.length; return chars.length;
} }
/**
* The toString method.
*
* @return the string
*/
public String toStringMethod() { public String toStringMethod() {
return this; return this;
} }
/**
* Get the java.lang.String.
*
* @return the string
*/
@Ignore @Ignore
public java.lang.String asString() { public java.lang.String asString() {
return new java.lang.String(chars); return new java.lang.String(chars);
} }
/**
* Wrap a java.lang.String.
*
* @param x the string
* @return the object
*/
@Ignore @Ignore
public static String wrap(java.lang.String x) { public static String wrap(java.lang.String x) {
return new String(x.toCharArray()); return new String(x.toCharArray());
......
...@@ -30,8 +30,8 @@ public class System { ...@@ -30,8 +30,8 @@ public class System {
*/ */
public static void arraycopy(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->getData()) + destPos, memmove(((jchar*)dest->getPointer()) + destPos,
((jchar*)src->getData()) + srcPos, sizeof(jchar) * length); ((jchar*)src->getPointer()) + srcPos, sizeof(jchar) * length);
*/ */
// c: return; // c: return;
java.lang.System.arraycopy(src, srcPos, dest, destPos, length); java.lang.System.arraycopy(src, srcPos, dest, destPos, length);
...@@ -49,8 +49,8 @@ public class System { ...@@ -49,8 +49,8 @@ public class System {
*/ */
public static void arraycopy(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->getData()) + destPos, memmove(((jbyte*)dest->getPointer()) + destPos,
((jbyte*)src->getData()) + srcPos, sizeof(jbyte) * length); ((jbyte*)src->getPointer()) + srcPos, sizeof(jbyte) * length);
*/ */
// c: return; // c: return;
java.lang.System.arraycopy(src, srcPos, dest, destPos, length); java.lang.System.arraycopy(src, srcPos, dest, destPos, length);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论