From: DeaDDooMER Date: Thu, 3 Aug 2017 10:18:53 +0000 (+0300) Subject: JVM: Исправлено сравнение LongInt, Real и LongReal X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=ffca530414fc4c94c3d95b9bc52f4d0cee3326e9;p=dsw-obn.git JVM: Исправлено сравнение LongInt, Real и LongReal --- diff --git a/jvm_test.sh b/jvm_test.sh index 6519656..0ec37e7 100755 --- a/jvm_test.sh +++ b/jvm_test.sh @@ -16,7 +16,7 @@ jasmin -d classes *.j javac -d classes rtl/*.java # -a -- for asm as comments -jad -o -b -noinner classes/* +#jad -o -b -noinner classes/* proguard -injars classes \ -libraryjars /usr/lib/jvm/java-8-openjdk/jre/lib/rt.jar \ diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index b17711b..0295e47 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -1521,18 +1521,10 @@ jvm_generate_abs(gen_proc_t * p, char prefix) jvm_generate(p, cell_size, cell_size, "invokestatic java/lang/Math/abs(%c)%c", t, t); } -static void -jvm_generate_compare_op(gen_proc_t * p, oberon_type_t * t, char prefix, int op) +static char * +jvm_get_compare_postfix(int op) { - int label_true = jvm_new_label_id(p); - int label_done = jvm_new_label_id(p); - char * label_name_true = jvm_get_label_name(label_true); - char * label_name_done = jvm_get_label_name(label_done); - int cell_size = 2 * jvm_cell_size_for_postfix(prefix); - - assert(prefix == 'i' || prefix == 'a'); - - const char * cmpop = ""; + char * cmpop = ""; switch(op) { case OP_EQ: @@ -1557,10 +1549,44 @@ jvm_generate_compare_op(gen_proc_t * p, oberon_type_t * t, char prefix, int op) gen_error("jvm_generate_compare_op: wat"); break; } + return cmpop; +} + +static void +jvm_generate_compare_op(gen_proc_t * p, oberon_type_t * t, int op) +{ + char prefix = jvm_get_prefix(t); + int label_true = jvm_new_label_id(p); + int label_done = jvm_new_label_id(p); + int cell_size = jvm_cell_size_for_type(t); + char * cmpop = jvm_get_compare_postfix(op); + + if(prefix == 'l') + { + jvm_generate(p, 2 * cell_size, 1, "lcmp"); + jvm_generate(p, 1, 1, "if%s L%i", cmpop, label_true); + } + else if(prefix == 'f' || prefix == 'd') + { + char fop; + if(op == OP_EQ || op == OP_NEQ || op == OP_GRT || op == OP_GEQ) + { + fop = 'l'; + } + else + { + fop = 'g'; + } + jvm_generate(p, 2 * cell_size, 1, "%ccmp%c", prefix, fop); + jvm_generate(p, 1, 1, "if%s L%i", cmpop, label_true); + } + else + { + jvm_generate(p, 2 * cell_size, 0, "if_%ccmp%s L%i", prefix, cmpop, label_true); + } - jvm_generate(p, cell_size, 0, "if_%ccmp%s %s", prefix, cmpop, label_name_true); jvm_generate(p, 0, 1, "iconst_0"); - jvm_generate(p, 0, 0, "goto %s", label_name_done); + jvm_generate(p, 0, 0, "goto L%i", label_done); jvm_generate_label(p, label_true); jvm_generate(p, 0, 1, "iconst_1"); jvm_generate_label(p, label_done); @@ -1617,7 +1643,7 @@ jvm_generate_operator(gen_proc_t * p, oberon_type_t * t, char prefix, int op) case OP_LEQ: case OP_GRT: case OP_GEQ: - jvm_generate_compare_op(p, t, prefix, op); + jvm_generate_compare_op(p, t, op); break; default: gen_error("jvm_generate_operator: unk op %i", op); diff --git a/src/oberon.c b/src/oberon.c index 5816c2b..f2c9014 100644 --- a/src/oberon.c +++ b/src/oberon.c @@ -2919,9 +2919,9 @@ oberon_statement(oberon_context_t * ctx) oberon_statement_seq(ctx); oberon_generate_goto(ctx, end); + oberon_generate_label(ctx, els); if(ctx -> token == ELSE) { - oberon_generate_label(ctx, els); oberon_assert_token(ctx, ELSE); oberon_statement_seq(ctx); } diff --git a/src/test.c b/src/test.c index a144f36..d250161 100644 --- a/src/test.c +++ b/src/test.c @@ -4,13 +4,14 @@ #include "../include/oberon.h" +/* static char source_test[] = "(* Main module *)" "MODULE Test;" "IMPORT Out;" "" "TYPE" - " R = INTEGER;" + " R = LONGINT;" "" "PROCEDURE Factorial(n : R) : R;" "BEGIN" @@ -39,6 +40,58 @@ static char source_test[] = " Out.Int(Factorial(12), 0); Out.Ln;" " Out.Int(Factorial(13), 0); Out.Ln;" " Out.Int(Factorial(14), 0); Out.Ln;" + " Out.Int(Factorial(15), 0); Out.Ln;" + " Out.Int(Factorial(16), 0); Out.Ln;" + " Out.Int(Factorial(17), 0); Out.Ln;" + " Out.Int(Factorial(18), 0); Out.Ln;" + " Out.Int(Factorial(19), 0); Out.Ln;" + " Out.Int(Factorial(20), 0); Out.Ln;" + "END Test." +; +*/ + +static char source_test[] = + "(* Main module *)" + "MODULE Test;" + "IMPORT Out;" + "" + "TYPE" + " R = LONGREAL;" + "" + "PROCEDURE Factorial(n : R) : R;" + "BEGIN" + " IF n <= 1 THEN" + " RETURN 1;" + " ELSE" + " RETURN n * Factorial(n - 1);" + " END;" + " RETURN 0; (* Детектор ретурнов - дерьмо *)" + "END Factorial;" + "" + "BEGIN" + " Out.Open();" + " Out.LongReal(Factorial(0), 0); Out.Ln;" + " Out.LongReal(Factorial(1), 0); Out.Ln;" + " Out.LongReal(Factorial(2), 0); Out.Ln;" + " Out.LongReal(Factorial(3), 0); Out.Ln;" + " Out.LongReal(Factorial(4), 0); Out.Ln;" + " Out.LongReal(Factorial(5), 0); Out.Ln;" + " Out.LongReal(Factorial(6), 0); Out.Ln;" + " Out.LongReal(Factorial(7), 0); Out.Ln;" + " Out.LongReal(Factorial(8), 0); Out.Ln;" + " Out.LongReal(Factorial(9), 0); Out.Ln;" + " Out.LongReal(Factorial(10), 0); Out.Ln;" + " Out.LongReal(Factorial(11), 0); Out.Ln;" + " Out.LongReal(Factorial(12), 0); Out.Ln;" + " Out.LongReal(Factorial(13), 0); Out.Ln;" + " Out.LongReal(Factorial(14), 0); Out.Ln;" + " Out.LongReal(Factorial(15), 0); Out.Ln;" + " Out.LongReal(Factorial(16), 0); Out.Ln;" + " Out.LongReal(Factorial(17), 0); Out.Ln;" + " Out.LongReal(Factorial(18), 0); Out.Ln;" + " Out.LongReal(Factorial(19), 0); Out.Ln;" + " Out.LongReal(Factorial(20), 0); Out.Ln;" + " Out.LongReal(Factorial(170), 0); Out.Ln;" "END Test." ;