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 \
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:
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);
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);
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);
}
#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"
" 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."
;