Skip to content

Commit

Permalink
fix issue #29 #30 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalgust committed Jan 20, 2024
1 parent 65e74a3 commit 222bc9d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 62 deletions.
8 changes: 5 additions & 3 deletions minijvm/c/jvm/class_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -1369,17 +1369,19 @@ void _changeBytesOrder(MethodInfo *method) {
memcpy(ca->bytecode_for_jit, ca->code, ca->code_length);

u8 *mc = ca->code;
if (ca->code_length == 5) {//optimize setter eg: void setSize(int size){this.size=size;}
if (ca->code_length == 5) {//optimize getter eg: void getSize(){return this.size;}
u8 mc4 = mc[4];
if (mc[1] == op_getfield
if (method->para_count_with_this == 1
&& mc[1] == op_getfield
&& mc[0] == op_aload_0
&& (mc4 >= op_ireturn && mc4 <= op_areturn)) {
method->is_getter = 1;
//jvm_printf(" getter %s.%s %d \n", utf8_cstr(method->_this_class->name), utf8_cstr(method->name), method->_this_class->status);
}
} else if (ca->code_length == 6) {//optimize setter eg: void setSize(int size){this.size=size;}
u8 mc1 = mc[1];
if (mc[5] == op_return
if (method->para_count_with_this == 2
&& mc[5] == op_return
&& mc[0] == op_aload_0
&& mc[2] == op_putfield
&& (mc1 == op_aload_1 || mc1 == op_dload_1 || mc1 == op_lload_1 || mc1 == op_iload_1 || mc1 == op_fload_1)) {
Expand Down
92 changes: 42 additions & 50 deletions minijvm/c/jvm/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,12 @@ static inline void _optimize_empty_method_call(MethodInfo *subm, CodeAttribute *
}


static inline int _optimize_inline_getter(JClass *clazz, s32 cmrIdx, Runtime *runtime) {
static inline int _optimize_inline_getter(JClass *clazz, s32 cfrIdx, Runtime *runtime) {

RuntimeStack *stack = runtime->stack;
FieldInfo *fi = class_get_constant_fieldref(clazz, cmrIdx)->fieldInfo;
FieldInfo *fi = class_get_constant_fieldref(clazz, cfrIdx)->fieldInfo;
if (!fi) {
ConstantFieldRef *cfr = class_get_constant_fieldref(clazz, cmrIdx);
ConstantFieldRef *cfr = class_get_constant_fieldref(clazz, cfrIdx);
fi = find_fieldInfo_by_fieldref(clazz, cfr->item.index, runtime);
cfr->fieldInfo = fi;
if (!fi) {
Expand Down Expand Up @@ -445,12 +445,12 @@ static inline int _optimize_inline_getter(JClass *clazz, s32 cmrIdx, Runtime *ru
return RUNTIME_STATUS_NORMAL;
}

static inline int _optimize_inline_setter(JClass *clazz, s32 cmrIdx, Runtime *runtime) {
static inline int _optimize_inline_setter(JClass *clazz, s32 cfrIdx, Runtime *runtime) {

RuntimeStack *stack = runtime->stack;
FieldInfo *fi = class_get_constant_fieldref(clazz, cmrIdx)->fieldInfo;
FieldInfo *fi = class_get_constant_fieldref(clazz, cfrIdx)->fieldInfo;
if (!fi) {
ConstantFieldRef *cfr = class_get_constant_fieldref(clazz, cmrIdx);
ConstantFieldRef *cfr = class_get_constant_fieldref(clazz, cfrIdx);
fi = find_fieldInfo_by_fieldref(clazz, cfr->item.index, runtime);
cfr->fieldInfo = fi;
if (!fi) {
Expand Down Expand Up @@ -1740,35 +1740,31 @@ s32 execute_method_impl(MethodInfo *method, Runtime *pruntime) {


case op_fdiv: {
if (!(sp - 1)->fvalue) {
goto label_arrithmetic_throw;
} else {
--sp;
(sp - 1)->fvalue /= (sp - 0)->fvalue;
// there isn't runtime exception throw
// https://github.com/digitalgust/miniJVM/issues/30
--sp;
(sp - 1)->fvalue /= (sp - 0)->fvalue;

#if _JVM_DEBUG_LOG_LEVEL > 5
invoke_deepth(runtime);
jvm_printf("fdiv: %f\n", (sp - 1)->fvalue);
invoke_deepth(runtime);
jvm_printf("fdiv: %f\n", (sp - 1)->fvalue);
#endif
ip++;
}
ip++;
break;
}

case op_ddiv: {
if (!(sp - 2)->dvalue) {
goto label_arrithmetic_throw;
} else {
--sp;
--sp;
(sp - 2)->dvalue /= (sp - 0)->dvalue;
// there isn't runtime exception throw
// https://github.com/digitalgust/miniJVM/issues/30
--sp;
--sp;
(sp - 2)->dvalue /= (sp - 0)->dvalue;

#if _JVM_DEBUG_LOG_LEVEL > 5
invoke_deepth(runtime);
jvm_printf("ddiv: %lf\n", (sp - 2)->dvalue);
invoke_deepth(runtime);
jvm_printf("ddiv: %lf\n", (sp - 2)->dvalue);
#endif
ip++;
}
ip++;
break;
}

Expand Down Expand Up @@ -1808,41 +1804,37 @@ s32 execute_method_impl(MethodInfo *method, Runtime *pruntime) {


case op_frem: {
if (!(sp - 1)->fvalue) {
goto label_arrithmetic_throw;
} else {
--sp;
fval1 = (sp - 0)->fvalue;
fval2 = (sp - 1)->fvalue;
f32 v = fval2 - ((int) (fval2 / fval1) * fval1);
// there isn't runtime exception throw
// https://github.com/digitalgust/miniJVM/issues/30
--sp;
fval1 = (sp - 0)->fvalue;
fval2 = (sp - 1)->fvalue;
f32 v = fval2 - ((int) (fval2 / fval1) * fval1);
#if _JVM_DEBUG_LOG_LEVEL > 5
invoke_deepth(runtime);
jvm_printf("frem: %f\n", (sp - 1)->fvalue);
invoke_deepth(runtime);
jvm_printf("frem: %f\n", (sp - 1)->fvalue);
#endif
(sp - 1)->fvalue = v;
ip++;
}
(sp - 1)->fvalue = v;
ip++;
break;
}


case op_drem: {
if (!(sp - 2)->dvalue) {
goto label_arrithmetic_throw;
} else {
--sp;
--sp;
dval1 = (sp - 0)->dvalue;
dval2 = (sp - 2)->dvalue;
f64 v = dval2 - ((s64) (dval2 / dval1) * dval1);;
// there isn't runtime exception throw
// https://github.com/digitalgust/miniJVM/issues/30
--sp;
--sp;
dval1 = (sp - 0)->dvalue;
dval2 = (sp - 2)->dvalue;
f64 v = dval2 - ((s64) (dval2 / dval1) * dval1);;

#if _JVM_DEBUG_LOG_LEVEL > 5
invoke_deepth(runtime);
jvm_printf("drem: %lf\n", (sp - 2)->dvalue);
invoke_deepth(runtime);
jvm_printf("drem: %lf\n", (sp - 2)->dvalue);
#endif
(sp - 2)->dvalue = v;
ip++;
}
(sp - 2)->dvalue = v;
ip++;
break;
}

Expand Down
2 changes: 1 addition & 1 deletion minijvm/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char **argv) {
utf8_append(bootcp, startup_dir);
utf8_append_c(bootcp, "../lib/minijvm_rt.jar");
bootclasspath = (c8 *) utf8_cstr(bootcp);
jdwp = 0; // 0:disable java debug , 1:enable java debug and disable jit
jdwp = 1; // 0:disable java debug , 1:enable java debug and disable jit

//test for graphics
utf8_append(cp, startup_dir);
Expand Down
24 changes: 18 additions & 6 deletions minijvm/java/src/main/java/org/mini/fs/FileSystemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ public boolean isAbsolute(File f) {
abstract String getRegexParentTag();

private String removeParentTag(String path) {
while (path.indexOf(getSeparator() + "..") >= 0) {
path = path.replaceAll(getRegexParentTag(), "");

if (path.endsWith(getSeparator() + PARENT_DIR)) { // "/tmp/.." -> "/tmp/../"
path = path + getSeparator();
}

// "/tmp/../a/b/../c" -> "/a/c"
while (path.indexOf(getSeparator() + PARENT_DIR) >= 0) {
path = path.replaceAll(getRegexParentTag(), getSeparator() + "");
}

return path;
Expand All @@ -50,9 +56,9 @@ public String normalize(String path) {
while (path.indexOf(ds) >= 0) {
path = path.replace(ds, ss);
}
path = path.replace(PARENT_DIR + getSeparator(), "\uffff\uffff\uffff");
path = path.replace(CUR_DIR + getSeparator(), ""); //remove all "./" to ""
path = path.replace("\uffff\uffff\uffff", PARENT_DIR + getSeparator());
path = path.replace(getSeparator() + PARENT_DIR + getSeparator(), "\uffff\uffff\uffff");
path = path.replace(getSeparator() + CUR_DIR + getSeparator(), getSeparator() + ""); //remove all "./" to ""
path = path.replace("\uffff\uffff\uffff", getSeparator() + PARENT_DIR + getSeparator());
if (path.length() > 1 && path.lastIndexOf(getSeparator()) == path.length() - 1) {//remove last char if it's '/'
path = path.substring(0, path.length() - 1);
}
Expand All @@ -66,7 +72,13 @@ protected String getFullPath(String path) {
}
path = removeParentTag(path);
path = normalize(path);
path = path.replace(getSeparator() + ".", "");
if (path.endsWith(getSeparator() + CUR_DIR)) { // "/tmp/abc/." -> "/tmp/abc/./"
path = path + getSeparator();
}
// https://github.com/digitalgust/miniJVM/issues/31
// "/tmp/abc/./" -> "/tmp/abc"
path = path.replace(getSeparator() + CUR_DIR + getSeparator(), getSeparator() + "");

return path;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean isAbsolute(String path) {
}

public String getRegexParentTag() {
return "/[^/]+/\\.\\.";
return "/[^/]+/\\.\\./";
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion minijvm/java/src/main/java/org/mini/fs/FileSystemWin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean isAbsolute(String path) {
}

public String getRegexParentTag() {
return "\\\\[^\\\\]+\\\\\\.\\.";
return "\\\\[^\\\\]+\\\\\\.\\.\\\\";
}

@Override
Expand Down

0 comments on commit 222bc9d

Please sign in to comment.