提交 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,10 +397,14 @@ class Type { ...@@ -392,10 +397,14 @@ 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()) {
...@@ -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;
}
} }
...@@ -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();
......
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论