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