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

Documentation / cleanup

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