DEADSOFTWARE

Исправлено приведение типа long -> char; Исправлено копирование полей базы расширяемо...
[dsw-obn.git] / src / backends / jvm / generator-jvm.c
index 1281c19426745d43a4982a24e92a26c834445bb7..681b4df71ddf6ea5f4c4c885016b2f1557002fea 100644 (file)
@@ -8,8 +8,8 @@
 
 #include <gc.h>
 
-#include "../../../include/oberon.h"
 #include "../../oberon-internals.h"
+#include "../../generator.h"
 #include "generator-jvm.h"
 #include "generator-jvm-abi.h"
 #include "generator-jvm-asm.h"
@@ -583,6 +583,13 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
        jvm_generate_var_initialization(p, copy_dst);
        jvm_generate_var_initialization(p, copy_src);
 
+       if(rec -> base)
+       {
+               jvm_generate_load(p, copy_dst);
+               jvm_generate_load(p, copy_src);
+               jvm_generate_copy_record(p, rec -> base);
+       }
+
        num = rec -> num_decl;
        field = rec -> decl;
        for(int i = 0; i < num; i++)
@@ -632,6 +639,13 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
        struct gen_class * c;
        char * name = var -> name;
        gen_type_t * t = var -> type -> gen_type;
+
+       assert(name);
+       if(t == NULL)
+       {
+               gen_error("uninitialized type class %i", var -> type -> class);
+       }
+
        switch(var -> class)
        {
                case OBERON_CLASS_VAR_PARAM:
@@ -725,6 +739,18 @@ oberon_generator_fini_module(oberon_module_t * mod)
 static void
 push_expr(gen_proc_t * p, oberon_expr_t * expr);
 
+static void
+oberon_generate_static_initialization(gen_proc_t * p, oberon_object_t * x)
+{
+       if(x -> type -> class == OBERON_TYPE_ARRAY
+               || x -> type -> class == OBERON_TYPE_RECORD)
+       {
+               jvm_generate_ldst_prepare(p, x -> gen_var);
+               jvm_generate_new(p, x -> type, 0);
+               jvm_generate_store(p, x -> gen_var);
+       }
+}
+
 void
 oberon_generate_begin_module(oberon_context_t * ctx)
 {
@@ -760,13 +786,7 @@ oberon_generate_begin_module(oberon_context_t * ctx)
                else if(x -> class == OBERON_CLASS_VAR)
                {
                        jvm_generate_var_initialization(p, x -> gen_var);
-                       if(x -> type -> class == OBERON_TYPE_ARRAY
-                               || x -> type -> class == OBERON_TYPE_RECORD)
-                       {
-                               jvm_generate_ldst_prepare(p, x -> gen_var);
-                               jvm_generate_new(p, x -> type, 0);
-                               jvm_generate_store(p, x -> gen_var);
-                       }
+                       oberon_generate_static_initialization(p, x);
                }
 
                x = x -> next;
@@ -865,10 +885,12 @@ jvm_generate_local_object(gen_proc_t * p, oberon_object_t * x)
                case OBERON_CLASS_VAR:
                        jvm_generate_var(v);
                        break;
+               case OBERON_CLASS_CONST:
+               case OBERON_CLASS_TYPE:
                case OBERON_CLASS_PROC:
                        break;
                default:
-                       gen_error("jvm_generate_local_initialization: wat");
+                       gen_error("jvm_generate_local_initialization: wat class %i", x -> class);
                        break;
        }
 }
@@ -889,28 +911,31 @@ jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x)
        switch(x -> class)
        {
                case OBERON_CLASS_VAR_PARAM:
+                       jvm_generate_var_initialization(p, v);
+                       break;
                case OBERON_CLASS_PARAM:
+                       jvm_generate_var_initialization(p, v);
+                       if(x -> type -> class == OBERON_TYPE_ARRAY)
+                       {
+                               jvm_generate_array_duplicate_and_replace(p, v, x -> type);
+                       }
+                       else if(x -> type -> class == OBERON_TYPE_RECORD)
+                       {                               
+                               jvm_generate_record_duplicate_and_replace(p, v, x -> type);
+                       }
+                       break;
                case OBERON_CLASS_VAR:
                        jvm_generate_var_initialization(p, v);
+                       oberon_generate_static_initialization(p, x);
                        break;
+               case OBERON_CLASS_CONST:
+               case OBERON_CLASS_TYPE:
                case OBERON_CLASS_PROC:
                        break;
                default:
-                       gen_error("jvm_generate_local_initialization: wat");
+                       gen_error("jvm_generate_local_initialization: wat class %i", x -> class);
                        break;
        }
-
-       if(x -> class == OBERON_CLASS_PARAM)
-       {
-               if(x -> type -> class == OBERON_TYPE_ARRAY)
-               {
-                       jvm_generate_array_duplicate_and_replace(p, v, x -> type);
-               }
-               else if(x -> type -> class == OBERON_TYPE_RECORD)
-               {                               
-                       jvm_generate_record_duplicate_and_replace(p, v, x -> type);
-               }
-       }
 }
 
 void
@@ -971,6 +996,7 @@ oberon_generate_end_proc(oberon_context_t * ctx)
        m = ctx -> mod -> gen_mod;
        p = m -> class -> p;
 
+       oberon_generate_trap(ctx, -4);
        jvm_generate_function_end(p);
 }
 
@@ -1178,11 +1204,22 @@ jvm_generate_expr_new_pointer(gen_proc_t * p, oberon_type_t * type, int num, obe
 static void
 jvm_generate_cast_prefix(gen_proc_t * p, char prefix, char postfix)
 {
+       if((prefix == 'b' || prefix == 's') && (postfix = 'l' || postfix == 'd'))
+       {
+               prefix = 'i';
+       }
+
        if(prefix == postfix)
        {
                return;
        }
 
+       if((prefix == 'l' || prefix == 'd') && (postfix == 'b' || postfix == 's'))
+       {
+               jvm_generate(p, 2, 1, "%c2i", prefix);
+               prefix = 'i';
+       }
+
        int from_cell_size = jvm_cell_size_for_postfix(prefix);
        int to_cell_size = jvm_cell_size_for_postfix(postfix);
        jvm_generate(p, from_cell_size, to_cell_size, "%c2%c", prefix, postfix);
@@ -1217,12 +1254,27 @@ jvm_generate_hard_cast_type(gen_proc_t * p, oberon_type_t * from, oberon_type_t
                if(from -> size <= 4)
                {
                        jvm_generate(p, 1, 1, "invokestatic java/lang/Float/floatToIntBits(F)I");
-                       jvm_generate_cast_prefix(p, 'I', postfix);
+                       jvm_generate_cast_prefix(p, 'i', postfix);
+               }
+               else
+               {
+                       jvm_generate(p, 2, 2, "invokestatic java/lang/Double/doubleToIntBits(D)J");
+                       jvm_generate_cast_prefix(p, 'l', postfix);
+               }
+       }
+       else if((from -> class == OBERON_TYPE_INTEGER || from -> class == OBERON_TYPE_SYSTEM_BYTE)
+               && to -> class == OBERON_TYPE_REAL)
+       {
+               char prefix = jvm_get_prefix(from);
+               if(to -> size <= 4)
+               {
+                       jvm_generate_cast_prefix(p, prefix, 'i');
+                       jvm_generate(p, 1, 1, "invokestatic java/lang/Float/intBitsToFloat(I)F");
                }
                else
                {
-                       jvm_generate(p, 2, 2, "invokestatic java/lang/Double/floatToIntBits(D)J");
-                       jvm_generate_cast_prefix(p, 'J', postfix);
+                       jvm_generate_cast_prefix(p, prefix, 'l');
+                       jvm_generate(p, 2, 2, "invokestatic java/lang/Double/longBitsToDouble(J)D");
                }
        }
        else