11 #include "../../../include/oberon.h"
12 #include "../../oberon-internals.h"
13 #include "generator-jvm.h"
14 #include "generator-jvm-basic.h"
16 // ==========================================
17 // ==========================================
18 // ==========================================
21 jvm_create_proc(struct gen_class
* class)
23 gen_proc_t
* p
= GC_MALLOC(sizeof *p
);
24 memset(p
, 0, sizeof *p
);
26 struct gen_register_file
* rf
= GC_MALLOC(sizeof *rf
);
27 memset(rf
, 0, sizeof *rf
);
29 struct gen_stack
* stack
= GC_MALLOC(sizeof *stack
);
30 memset(rf
, 0, sizeof *stack
);
41 jvm_generate_function_header(gen_proc_t
* p
, char * access
, char * name
, char * signature
)
43 struct gen_class
* class;
46 /* Делаем процедуру текущей в этом классе */
47 assert(class -> p
== NULL
);
50 fprintf(class -> fp
, ".method %s %s%s\n", access
, name
, signature
);
51 fprintf(class -> fp
, " start:\n");
55 jvm_generate_function_end(gen_proc_t
* p
)
57 struct gen_class
* class;
63 int pointer
= p
-> stack
-> pointer
;
64 int max_pointer
= p
-> stack
-> max_pointer
;
65 int locals
= p
-> rf
-> num_used
;
66 int max_locals
= p
-> rf
-> max_used
;
68 fprintf(class -> fp
, " .limit stack %i \t; current(%i)\n", max_pointer
, pointer
);
69 fprintf(class -> fp
, " .limit locals %i \t; current(%i)\n", max_locals
, locals
);
70 fprintf(class -> fp
, " end:\n");
71 fprintf(class -> fp
, ".end method\n\n");
75 jvm_create_class(char * full_name
)
77 struct gen_class
* class = GC_MALLOC(sizeof *class);
78 memset(class, 0, sizeof *class);
80 char * fname
= new_string("%s.j", full_name
);
81 class -> full_name
= new_string(full_name
);
82 class -> fp
= fopen(fname
, "w");
89 jvm_destroy_class(struct gen_class
* class)
91 assert(class -> p
== NULL
);
96 jvm_stack_push(gen_proc_t
* p
, unsigned size
)
98 p
-> stack
-> pointer
+= size
;
99 if(p
-> stack
-> pointer
> p
-> stack
-> max_pointer
)
101 p
-> stack
-> max_pointer
= p
-> stack
-> pointer
;
106 jvm_stack_pop(gen_proc_t
* p
, unsigned size
)
108 p
-> stack
-> pointer
-= size
;
110 if(p
-> stack
-> pointer
< 0)
112 printf("WARING: stack pointer %i\n", p
-> stack
-> pointer
);
117 jvm_generate(gen_proc_t
* p
, unsigned get
, unsigned push
, char * format
, ...)
120 va_start(ptr
, format
);
122 jvm_stack_pop(p
, get
);
123 fprintf(p
-> class -> fp
, " ");
124 vfprintf(p
-> class -> fp
, format
, ptr
);
125 jvm_stack_push(p
, push
);
126 fprintf(p
-> class -> fp
, " \t ;>>>> %i -- %i : current_stack(%i)\n", get
, push
, p
-> stack
-> pointer
);
132 jvm_generate_comment(gen_proc_t
* p
, char * format
, ...)
135 va_start(ptr
, format
);
137 fprintf(p
-> class -> fp
, " ;;;; ");
138 vfprintf(p
-> class -> fp
, format
, ptr
);
139 fprintf(p
-> class -> fp
, "\n");
145 jvm_generate_push_int(gen_proc_t
* p
, int64_t i
)
149 jvm_generate(p
, 0, 1, "iconst_m1");
151 else if(i
>= 0 && i
<= 5)
153 jvm_generate(p
, 0, 1, "iconst_%li", i
);
155 else if(i
>= -128 && i
<= 127)
157 jvm_generate(p
, 0, 1, "bipush %li", i
);
159 else if(i
>= -32768 && i
<= 32767)
161 jvm_generate(p
, 0, 1, "sipush %li", i
);
163 else if(i
>= -2147483648 && i
<= 2147483647)
165 jvm_generate(p
, 0, 1, "ldc %li", i
);
169 jvm_generate(p
, 0, 2, "ldc2 %li", i
);
174 jvm_generate_push_int_size(gen_proc_t
* p
, int64_t i
, int size
)
180 jvm_generate(p
, 0, 1, "iconst_m1");
182 else if(i
>= 0 && i
<= 5)
184 jvm_generate(p
, 0, 1, "iconst_%li", i
);
186 else if(i
>= -128 && i
<= 127)
188 jvm_generate(p
, 0, 1, "bipush %li", i
);
190 else if(i
>= -32768 && i
<= 32767)
192 jvm_generate(p
, 0, 1, "sipush %li", i
);
194 else if(i
>= -2147483648 && i
<= 2147483647)
196 jvm_generate(p
, 0, 1, "ldc %li", i
);
201 jvm_generate(p
, 0, 2, "ldc2 %li", i
);
205 if(size
> 4 && pushed_cell
== 1)
207 jvm_generate(p
, pushed_cell
, 2, "i2l");
213 jvm_generate(p
, 2, 1, "l2i");
218 jvm_generate(p
, 1, 1, "i2s");
222 jvm_generate(p
, 1, 1, "i2b");
228 jvm_generate_push_float(gen_proc_t
* p
, double f
, int size
)
234 jvm_generate(p
, 0, 1, "fconst_0");
238 jvm_generate(p
, 0, 1, "fconst_1");
242 jvm_generate(p
, 0, 1, "fconst_2");
246 jvm_generate(p
, 0, 1, "ldc %lf", f
);
253 jvm_generate(p
, 0, 2, "dconst_0");
257 jvm_generate(p
, 0, 2, "dconst_1");
261 jvm_generate(p
, 0, 2, "ldc2 %lf", f
);
267 oberon_generator_new_var()
269 gen_var_t
* v
= GC_MALLOC(sizeof *v
);
270 memset(v
, 0, sizeof *v
);
275 jvm_generate_and_init_global_var(gen_var_t
* v
, struct gen_class
* class, char * access
, char * name
, char * desc
)
277 assert(class -> p
== NULL
);
278 fprintf(class -> fp
, ".field %s static %s %s\n\n", access
, name
, desc
);
280 v
-> storage
= JVM_STORAGE_STATIC
;
281 v
-> full_name
= new_string("%s/%s", class -> full_name
, name
);
282 v
-> desc
= new_string(desc
);
286 jvm_generate_and_init_field(gen_var_t
* v
, struct gen_class
* class, char * access
, char * name
, char * desc
)
288 assert(class -> p
== NULL
);
289 fprintf(class -> fp
, ".field %s %s %s\n\n", access
, name
, desc
);
291 v
-> storage
= JVM_STORAGE_FIELD
;
292 v
-> full_name
= new_string("%s/%s", class -> full_name
, name
);
293 v
-> desc
= new_string(desc
);
297 jvm_generate_and_init_local_var(gen_var_t
* v
, gen_proc_t
* p
, bool wide
)
299 v
-> storage
= JVM_STORAGE_REGISTER
;
300 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, wide
);
304 jvm_generate_and_init_named_local_var(gen_var_t
* v
, gen_proc_t
* p
, bool wide
, char * name
, char * desc
)
306 jvm_generate_and_init_local_var(v
, p
, wide
);
307 v
-> desc
= new_string(desc
);
308 jvm_generate(p
, 0, 0, ".var %i is %s %s from start to end", v
-> reg
, name
, desc
);
312 jvm_new_label_id(gen_proc_t
* p
)
314 int label_id
= p
-> label_id
;
320 jvm_get_label_name(int label_id
)
322 return new_string("L%i", label_id
);
326 jvm_generate_label(gen_proc_t
* p
, int label_id
)
328 jvm_generate(p
, 0, 0, "L%i:", label_id
);
332 jvm_generate_load(gen_proc_t
* p
, oberon_type_t
* src_type
, gen_var_t
* src
)
337 cell_size
= jvm_cell_size_for_type(src_type
);
339 switch(src
-> storage
)
341 case JVM_STORAGE_REGISTER
:
342 prefix
= jvm_get_prefix(src_type
);
343 jvm_generate(p
, 0, cell_size
, "%cload %i", prefix
, src
-> reg
);
345 case JVM_STORAGE_STATIC
:
346 jvm_generate(p
, 0, cell_size
, "getstatic %s %s", src
-> full_name
, src
-> desc
);
348 case JVM_STORAGE_FIELD
:
349 jvm_generate(p
, 1, cell_size
, "getfield %s %s", src
-> full_name
, src
-> desc
);
352 gen_error("jvm_generate_load: unknow storage type %i", src
-> storage
);
358 jvm_generate_store(gen_proc_t
* p
, oberon_type_t
* src_type
, gen_var_t
* dst
)
363 cell_size
= jvm_cell_size_for_type(src_type
);
365 switch(dst
-> storage
)
367 case JVM_STORAGE_REGISTER
:
368 prefix
= jvm_get_prefix(src_type
);
369 jvm_generate(p
, cell_size
, 0, "%cstore %i", prefix
, dst
-> reg
);
371 case JVM_STORAGE_STATIC
:
372 jvm_generate(p
, cell_size
, 0, "putstatic %s %s", dst
-> full_name
, dst
-> desc
);
374 case JVM_STORAGE_FIELD
:
375 jvm_generate(p
, 1 + cell_size
, 0, "putfield %s %s", dst
-> full_name
, dst
-> desc
);
378 gen_error("jvm_generate_store: unknow storage type %i", dst
-> storage
);
384 jvm_generate_new(gen_proc_t
* p
, oberon_type_t
* type
, int num
);
387 * Генерирует код для инициализации массива со статическим базовым типом
392 jvm_generate_array_initialization(gen_proc_t
* p
, oberon_type_t
* arr
)
395 oberon_type_t
* base
= arr
;
396 while(base
-> class == OBERON_TYPE_ARRAY
)
402 if(base
-> class != OBERON_TYPE_RECORD
)
404 jvm_generate(p
, 1, 0, "pop");
415 gen_var_t
* dst
= oberon_generator_new_var();
416 jvm_generate_and_init_local_var(dst
, p
, false);
417 jvm_generate_store(p
, arr
, dst
);
420 * Входящие параметры заграблены.
421 * Теперь генерируем эквивалентный код:
423 * int len = dst.length
428 * dst[i, ...] = new record;
433 * Где "..." такой же код (начало и конец) для следующей размерности.
436 for(int i
= 0; i
< dim
; i
++)
438 loop
[i
].index
= oberon_generator_new_var();
439 loop
[i
].length
= oberon_generator_new_var();
440 loop
[i
].start
= jvm_new_label_id(p
);
441 loop
[i
].end
= jvm_new_label_id(p
);
442 jvm_generate_and_init_local_var(loop
[i
].index
, p
, false);
443 jvm_generate_and_init_local_var(loop
[i
].length
, p
, false);
445 /* TODO преределать через jvm_generate_load{{store}} */
447 jvm_generate(p
, 0, 1, "iconst_0");
448 jvm_generate(p
, 1, 0, "istore %i", loop
[i
].index
-> reg
);
450 jvm_generate_load(p
, arr
, dst
);
451 jvm_generate(p
, 1, 1, "arraylength");
452 jvm_generate(p
, 1, 0, "istore %i", loop
[i
].length
-> reg
);
454 /* if(i >= len) goto end; */
455 jvm_generate_label(p
, loop
[i
].start
);
456 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
457 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].length
-> reg
);
458 jvm_generate(p
, 2, 0, "if_icmpge L%i", loop
[i
].end
);
461 jvm_generate_load(p
, arr
, dst
);
462 jvm_generate(p
, 0, 1, "iload %i", loop
[0].index
-> reg
);
463 for(int i
= 1; i
< dim
; i
++)
465 jvm_generate(p
, 2, 1, "aaload");
466 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
468 jvm_generate_new(p
, base
, 0);
469 jvm_generate(p
, 3, 0, "aastore");
471 for(int i
= dim
- 1; i
>= 0; i
--)
473 jvm_generate(p
, 0, 0, "iinc %i 1", loop
[i
].index
-> reg
);
474 jvm_generate(p
, 0, 0, "goto L%i", loop
[i
].start
);
475 jvm_generate_label(p
, loop
[i
].end
);
480 jvm_generate_new(gen_proc_t
* p
, oberon_type_t
* type
, int num
)
485 oberon_type_t
* base
;
487 switch(type
-> class)
489 case OBERON_TYPE_INTEGER
:
490 case OBERON_TYPE_BOOLEAN
:
491 case OBERON_TYPE_PROCEDURE
:
492 case OBERON_TYPE_REAL
:
493 case OBERON_TYPE_POINTER
:
494 gen_error("jvm_generate_new_static: static alocation not allowed");
496 case OBERON_TYPE_RECORD
:
498 cname
= jvm_get_class_full_name(type
);
499 jvm_generate(p
, 0, 1, "new %s", cname
);
500 jvm_generate(p
, 1, 2, "dup");
501 jvm_generate(p
, 1, 0, "invokespecial %s/<init>()V", cname
);
503 case OBERON_TYPE_ARRAY
:
506 desc
= jvm_get_descriptor(type
);
507 while(base
-> class == OBERON_TYPE_ARRAY
)
511 assert(base
-> size
== 0);
516 assert(base
-> size
> 0);
517 jvm_generate_push_int(p
, base
-> size
);
524 jvm_generate(p
, dim
, 1, "multianewarray %s %i", desc
, dim
);
525 jvm_generate(p
, 1, 2, "dup");
526 jvm_generate_array_initialization(p
, type
);
529 gen_error("jvm_generate_new_static: unk type class %i", type
-> class);
535 * Генерирует код для копирования полей из первой записи во вторую.
536 * ( aref_dst aref_src -- )
541 jvm_generate_copy_record(gen_proc_t
* p
, oberon_type_t
* rec
)
543 assert(rec
-> class == OBERON_TYPE_RECORD
);
544 char * desc
= jvm_get_descriptor(rec
);
545 char * cname
= jvm_get_class_full_name(rec
);
546 jvm_generate(p
, 1 + 1, 0, "invokestatic %s/$COPY$(%s%s)V", cname
, desc
, desc
);
550 * Генерирует кода для копирования массивов.
551 * ( aref_dst aref_src -- )
556 jvm_generate_copy_array(gen_proc_t
* p
, oberon_type_t
* arr
)
559 oberon_type_t
* base
= arr
;
560 while(base
-> class == OBERON_TYPE_ARRAY
)
573 gen_var_t
* dst
= oberon_generator_new_var();
574 gen_var_t
* src
= oberon_generator_new_var();
575 jvm_generate_and_init_local_var(dst
, p
, false);
576 jvm_generate_and_init_local_var(src
, p
, false);
578 jvm_generate_store(p
, arr
, src
);
579 jvm_generate_store(p
, arr
, dst
);
582 * Входящие параметры заграблены.
583 * Теперь генерируем эквивалентный код:
585 * int len = dst.length
590 * copy from src[i, ...] to dst[i, ...];
595 * Где "..." такой же код (начало и конец) для следующей размерности.
598 for(int i
= 0; i
< dim
; i
++)
600 loop
[i
].index
= oberon_generator_new_var();
601 loop
[i
].length
= oberon_generator_new_var();
602 loop
[i
].start
= jvm_new_label_id(p
);
603 loop
[i
].end
= jvm_new_label_id(p
);
604 jvm_generate_and_init_local_var(loop
[i
].index
, p
, false);
605 jvm_generate_and_init_local_var(loop
[i
].length
, p
, false);
607 /* TODO преределать через jvm_generate_load{{store}} */
609 jvm_generate(p
, 0, 1, "iconst_0");
610 jvm_generate(p
, 1, 0, "istore %i", loop
[i
].index
-> reg
);
612 jvm_generate_load(p
, arr
, dst
);
613 jvm_generate(p
, 1, 1, "arraylength");
614 jvm_generate(p
, 1, 0, "istore %i", loop
[i
].length
-> reg
);
616 /* if(i >= len) goto end; */
617 jvm_generate_label(p
, loop
[i
].start
);
618 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
619 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].length
-> reg
);
620 jvm_generate(p
, 2, 0, "if_icmpge L%i", loop
[i
].end
);
623 if(base
-> class == OBERON_TYPE_RECORD
)
625 /* Получаем записи по индексам ( -- dst src ) */
627 jvm_generate_load(p
, arr
, dst
);
628 for(int i
= 0; i
< dim
; i
++)
630 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
631 jvm_generate(p
, 2, 1, "aaload");
634 jvm_generate_load(p
, arr
, src
);
635 for(int i
= 0; i
< dim
; i
++)
637 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
638 jvm_generate(p
, 2, 1, "aaload");
641 /* Копируем записи ( dst src -- ) */
642 jvm_generate_copy_record(p
, base
);
646 char postfix
= jvm_get_postfix(base
);
647 int cell_size
= jvm_cell_size_for_postfix(postfix
);
649 /* Получаем массивы и индексы ( -- dst i src i ) */
651 jvm_generate_load(p
, arr
, dst
);
652 jvm_generate(p
, 0, 1, "iload %i", loop
[0].index
-> reg
);
653 for(int i
= 1; i
< dim
; i
++)
655 jvm_generate(p
, 2, 1, "aaload");
656 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
659 jvm_generate_load(p
, arr
, src
);
660 jvm_generate(p
, 0, 1, "iload %i", loop
[0].index
-> reg
);
661 for(int i
= 1; i
< dim
; i
++)
663 jvm_generate(p
, 2, 1, "aaload");
664 jvm_generate(p
, 0, 1, "iload %i", loop
[i
].index
-> reg
);
667 /* Копируем значения ( dst i src i -- ) */
668 jvm_generate(p
, 2, cell_size
, "%caload", postfix
);
669 jvm_generate(p
, 2 + cell_size
, 0, "%castore", postfix
);
672 for(int i
= dim
- 1; i
>= 0; i
--)
674 jvm_generate(p
, 0, 0, "iinc %i 1", loop
[i
].index
-> reg
);
675 jvm_generate(p
, 0, 0, "goto L%i", loop
[i
].start
);
676 jvm_generate_label(p
, loop
[i
].end
);
681 jvm_generate_push_procedure_pointer(gen_proc_t
* p
, oberon_object_t
* proc
)
683 char * full_name
= jvm_get_field_full_name_safe(proc
);
684 char * desc
= jvm_get_descriptor(proc
-> type
);
685 jvm_generate(p
, 0, 1, "getstatic %s/pointer %s", full_name
, desc
);
688 // ==========================================
689 // ==========================================
690 // ==========================================
693 oberon_generator_init_context(oberon_context_t
* ctx
)
695 gen_context_t
* gen_context
= GC_MALLOC(sizeof *gen_context
);
696 memset(gen_context
, 0, sizeof *gen_context
);
698 ctx
-> gen_context
= gen_context
;
702 oberon_generator_destroy_context(oberon_context_t
* ctx
)
707 oberon_generate_procedure_class(oberon_type_t
* proc
)
712 struct gen_class
* class;
714 cname
= jvm_get_class_full_name(proc
);
715 class = jvm_create_class(cname
);
718 fprintf(fp
, ".source SYSTEM\n");
719 fprintf(fp
, ".class public abstract %s\n", cname
);
720 fprintf(fp
, ".super java/lang/Object\n\n");
722 fprintf(fp
, ".method <init>()V\n");
723 fprintf(fp
, " aload_0\n");
724 fprintf(fp
, " invokespecial java/lang/Object/<init>()V\n");
725 fprintf(fp
, " return\n");
726 fprintf(fp
, ".end method\n\n");
728 signature
= jvm_get_procedure_signature(proc
);
730 fprintf(fp
, ".method public abstract invoke%s\n", signature
);
731 fprintf(fp
, ".end method\n\n");
733 jvm_destroy_class(class);
737 oberon_generate_procedure_pointer_class(oberon_object_t
* proc
)
744 struct gen_class
* class;
746 cname
= jvm_get_field_full_name_safe(proc
);
747 class = jvm_create_class(cname
);
748 abscname
= jvm_get_class_full_name(proc
-> type
);
749 absdesc
= jvm_get_descriptor(proc
-> type
);
752 fprintf(fp
, ".source %s\n", proc
-> module
-> name
);
753 fprintf(fp
, ".class public %s\n", cname
);
754 fprintf(fp
, ".super %s\n\n", abscname
);
756 fprintf(fp
, ".field public static pointer %s\n\n", absdesc
);
758 fprintf(fp
, ".method <init>()V\n");
759 fprintf(fp
, " aload_0\n");
760 fprintf(fp
, " invokespecial %s/<init>()V\n", abscname
);
761 fprintf(fp
, " aload_0\n");
762 fprintf(fp
, " putstatic %s/pointer %s\n", cname
, absdesc
);
763 fprintf(fp
, " return\n");
764 fprintf(fp
, ".end method\n\n");
766 signature
= jvm_get_procedure_signature(proc
-> type
);
771 p
= jvm_create_proc(class);
772 jvm_generate_function_header(p
, "public", "invoke", signature
);
774 this_v
= oberon_generator_new_var();
775 jvm_generate_and_init_local_var(this_v
, p
, false);
779 int num
= proc
-> type
-> num_decl
;
780 oberon_object_t
* arg
= proc
-> type
-> decl
;
781 for(int i
= 0; i
< num
; i
++)
783 v
= oberon_generator_new_var();
784 bool wide
= jvm_is_wide_type(arg
-> type
);
785 char * desc
= jvm_get_descriptor(arg
-> type
);
786 jvm_generate_and_init_named_local_var(v
, p
, wide
, arg
-> name
, desc
);
788 jvm_generate_load(p
, arg
-> type
, v
);
790 use_size
+= (wide
) ? (2) : (1);
795 char * full_name
= jvm_get_field_full_name(proc
);
796 int cell_size
= jvm_cell_size_for_type(proc
-> type
-> base
);
798 jvm_generate(p
, use_size
, cell_size
, "invokestatic %s%s", full_name
, signature
);
800 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
)
802 jvm_generate(p
, 0, 0, "return");
806 char prefix
= jvm_get_prefix(proc
-> type
-> base
);
807 jvm_generate(p
, cell_size
, 0, "%creturn", prefix
);
810 jvm_generate_function_end(p
);
812 jvm_destroy_class(class);
816 oberon_generate_record_class(gen_module_t
* m
, oberon_type_t
* rec
)
819 struct gen_class
* class;
821 /* Устанавливаем новоый id */
822 rec
-> gen_type
-> rec_id
= m
-> rec_id
;
825 cname
= jvm_get_class_full_name(rec
);
826 class = jvm_create_class(cname
);
828 fprintf(class -> fp
, ".source %s\n", rec
-> module
-> name
);
829 fprintf(class -> fp
, ".class public %s\n", cname
);
830 fprintf(class -> fp
, ".super java/lang/Object\n\n");
832 rec
-> gen_type
-> class = class;
836 oberon_generator_init_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
838 gen_type_t
* t
= GC_MALLOC(sizeof *t
);
839 memset(t
, 0, sizeof *t
);
840 type
-> gen_type
= t
;
842 switch(type
-> class)
844 case OBERON_TYPE_VOID
:
845 case OBERON_TYPE_INTEGER
:
846 case OBERON_TYPE_BOOLEAN
:
847 case OBERON_TYPE_ARRAY
:
848 case OBERON_TYPE_REAL
:
850 case OBERON_TYPE_RECORD
:
853 m
= type
-> module
-> gen_mod
;
854 oberon_generate_record_class(m
, type
);
856 case OBERON_TYPE_PROCEDURE
:
857 oberon_generate_procedure_class(type
);
859 case OBERON_TYPE_POINTER
:
862 gen_error("oberon_generator_init_type: unk calss %i", type
-> class);
868 oberon_generator_init_record(oberon_context_t
* ctx
, oberon_type_t
* rec
)
870 struct gen_class
* class;
871 class = rec
-> gen_type
-> class;
873 int num
= rec
-> num_decl
;
874 oberon_object_t
* field
= rec
-> decl
;
875 for(int i
= 0; i
< num
; i
++)
877 gen_var_t
* v
= field
-> gen_var
;
878 char * name
= field
-> name
;
879 char * desc
= jvm_get_descriptor(field
-> type
);
880 jvm_generate_and_init_field(v
, class, "public", name
, desc
);
881 field
= field
-> next
;
884 /* Стандартный конструктор класса */
885 /* Инициализирует внутренние статические записи и массивы */
886 gen_proc_t
* p
= jvm_create_proc(class);
887 jvm_generate_function_header(p
, "public", "<init>", "()V");
888 jvm_alloc_register_untyped(p
-> rf
, false);
889 jvm_generate(p
, 0, 1, "aload_0");
890 jvm_generate(p
, 1, 0, "invokespecial java/lang/Object/<init>()V");
891 num
= rec
-> num_decl
;
893 for(int i
= 0; i
< num
; i
++)
895 if(field
-> type
-> class == OBERON_TYPE_RECORD
896 || field
-> type
-> class == OBERON_TYPE_ARRAY
)
898 jvm_generate(p
, 0, 1, "aload_0");
899 jvm_generate_new(p
, field
-> type
, 0);
900 jvm_generate_store(p
, field
-> type
, field
-> gen_var
);
902 field
= field
-> next
;
904 jvm_generate(p
, 0, 0, "return");
905 jvm_generate_function_end(p
);
907 /* Метод для копирования полей класса */
908 /* reg0 == src -> reg1 == dst */
909 p
= jvm_create_proc(class);
910 gen_var_t
* copy_dst
= oberon_generator_new_var();
911 gen_var_t
* copy_src
= oberon_generator_new_var();
912 char * desc
= jvm_get_descriptor(rec
);
913 char * signature
= new_string("(%s%s)V", desc
, desc
);
914 jvm_generate_function_header(p
, "public static", "$COPY$", signature
);
915 jvm_generate_and_init_named_local_var(copy_dst
, p
, false, "dst", desc
);
916 jvm_generate_and_init_named_local_var(copy_src
, p
, false, "src", desc
);
917 num
= rec
-> num_decl
;
919 for(int i
= 0; i
< num
; i
++)
921 if(field
-> type
-> class == OBERON_TYPE_RECORD
)
923 jvm_generate_load(p
, rec
, copy_dst
);
924 jvm_generate_load(p
, field
-> type
, field
-> gen_var
);
925 jvm_generate_load(p
, rec
, copy_src
);
926 jvm_generate_load(p
, field
-> type
, field
-> gen_var
);
927 jvm_generate_copy_record(p
, field
-> type
);
929 else if(field
-> type
-> class == OBERON_TYPE_ARRAY
)
931 jvm_generate_load(p
, rec
, copy_dst
);
932 jvm_generate_load(p
, field
-> type
, field
-> gen_var
);
933 jvm_generate_load(p
, rec
, copy_src
);
934 jvm_generate_load(p
, field
-> type
, field
-> gen_var
);
935 jvm_generate_copy_array(p
, field
-> type
);
939 jvm_generate_load(p
, rec
, copy_dst
);
940 jvm_generate_load(p
, rec
, copy_src
);
941 jvm_generate_load(p
, field
-> type
, field
-> gen_var
);
942 jvm_generate_store(p
, field
-> type
, field
-> gen_var
);
944 field
= field
-> next
;
946 jvm_generate(p
, 0, 0, "return");
947 jvm_generate_function_end(p
);
949 jvm_destroy_class(class);
953 oberon_generator_init_var(oberon_context_t
* ctx
, oberon_object_t
* var
)
955 gen_var_t
* v
= oberon_generator_new_var();
959 m
= ctx
-> mod
-> gen_mod
;
961 struct gen_class
* class;
967 case OBERON_CLASS_VAR_PARAM
:
968 gen_error("generator: VAR-parameters not implemented");
970 case OBERON_CLASS_PARAM
:
971 /* Заполняется при генерации функции */
972 /* смотри jvm_init_local_object() */
974 case OBERON_CLASS_FIELD
:
975 /* Заполняются при инициализации структуры */
976 /* смотри oberon_generator_init_record() */
978 case OBERON_CLASS_VAR
:
979 /* Локальные заполняются при генерации функции */
980 /* смотри jvm_init_local_object() */
981 if(var
-> local
== 0)
983 desc
= jvm_get_descriptor(var
-> type
);
984 jvm_generate_and_init_global_var(v
, class, "public", var
-> name
, desc
);
988 gen_error("oberon_generator_init_var: unk var class %i", var
-> class);
994 oberon_generator_init_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
996 struct gen_class
* class = proc
-> module
-> gen_mod
-> class;
997 proc
-> gen_proc
= jvm_create_proc(class);
1001 gen_error("generator: local procedures not implemented");
1004 oberon_generate_procedure_pointer_class(proc
);
1008 oberon_generator_init_module(oberon_context_t
* ctx
, oberon_module_t
* mod
)
1010 gen_module_t
* m
= GC_MALLOC(sizeof *m
);
1011 memset(m
, 0, sizeof *m
);
1014 struct gen_class
* class;
1015 class = jvm_create_class(mod
-> name
);
1017 fprintf(class -> fp
, ".source %s\n", mod
-> name
);
1018 fprintf(class -> fp
, ".class %s\n", mod
-> name
);
1019 fprintf(class -> fp
, ".super java/lang/Object\n\n");
1023 ctx
-> gen_context
-> current_m
= m
;
1027 oberon_generator_fini_module(oberon_module_t
* mod
)
1029 jvm_destroy_class(mod
-> gen_mod
-> class);
1033 push_expr(gen_proc_t
* p
, oberon_expr_t
* expr
);
1036 oberon_generate_begin_module(oberon_context_t
* ctx
)
1038 struct gen_class
* class = ctx
-> mod
-> gen_mod
-> class;
1039 gen_proc_t
* p
= jvm_create_proc(class);
1041 jvm_generate_function_header(p
, "public", "<init>", "()V");
1042 jvm_alloc_register_untyped(p
-> rf
, false);
1043 jvm_generate(p
, 0, 1, "aload_0");
1044 jvm_generate(p
, 1, 0, "invokespecial java/lang/Object/<init>()V");
1046 /* Инициализация переменных объявленных в модуле */
1047 oberon_object_t
* x
= ctx
-> mod
-> decl
-> list
-> next
;
1050 if(x
-> class == OBERON_CLASS_VAR
)
1052 if(x
-> type
-> class == OBERON_TYPE_ARRAY
1053 || x
-> type
-> class == OBERON_TYPE_RECORD
)
1055 jvm_generate_new(p
, x
-> type
, 0);
1056 jvm_generate_store(p
, x
-> type
, x
-> gen_var
);
1059 else if(x
-> class == OBERON_CLASS_PROC
)
1061 char * cname
= jvm_get_field_full_name_safe(x
);
1062 jvm_generate(p
, 0, 1, "new %s", cname
);
1063 jvm_generate(p
, 1, 0, "invokespecial %s/<init>()V", cname
);
1071 oberon_generate_end_module(oberon_context_t
* ctx
)
1073 struct gen_class
* class = ctx
-> mod
-> gen_mod
-> class;
1074 gen_proc_t
* p
= class -> p
;
1076 jvm_generate(p
, 0, 0, "return");
1077 jvm_generate_function_end(class -> p
);
1081 jvm_init_local_object(gen_proc_t
* p
, oberon_object_t
* x
)
1086 struct gen_class
* class;
1090 wide
= jvm_is_wide_type(x
-> type
);
1091 desc
= jvm_get_descriptor(x
-> type
);
1093 // Убеждаемся что сейчас находимся в функции
1099 case OBERON_CLASS_VAR
:
1100 case OBERON_CLASS_PARAM
:
1101 jvm_generate_and_init_named_local_var(v
, p
, wide
, x
-> name
, desc
);
1104 gen_error("jvm_init_local_object: wat");
1110 * Генерирует код для получения размера измерения массива
1111 * Аналог Обероновского LEN(v, n); где n = 0 - первое измерение.
1116 jvm_generate_array_len(gen_proc_t
* p
, int dim
)
1120 jvm_generate(p
, 0, 1, "iconst_0");
1121 jvm_generate(p
, 2, 1, "aaload");
1124 jvm_generate(p
, 1, 1, "arraylength");
1128 jvm_generate_array_duplicate_and_replace(gen_proc_t
* p
, gen_var_t
* v
, oberon_type_t
* arr
)
1131 oberon_type_t
* base
= arr
;
1132 while(base
-> class == OBERON_TYPE_ARRAY
)
1134 if(base
-> size
== 0)
1136 jvm_generate_load(p
, arr
, v
);
1137 jvm_generate_array_len(p
, dim
);
1140 base
= base
-> base
;
1143 jvm_generate_new(p
, arr
, dim
);
1144 jvm_generate(p
, 1, 2, "dup");
1145 jvm_generate_load(p
, arr
, v
);
1146 jvm_generate_copy_array(p
, arr
);
1147 jvm_generate_store(p
, arr
, v
);
1151 jvm_generate_record_duplicate_and_replace(gen_proc_t
* p
, gen_var_t
* v
, oberon_type_t
* rec
)
1153 jvm_generate_new(p
, rec
, 0);
1154 jvm_generate(p
, 1, 2, "dup");
1155 jvm_generate_load(p
, rec
, v
);
1156 jvm_generate_copy_record(p
, rec
);
1157 jvm_generate_store(p
, rec
, v
);
1161 oberon_generate_begin_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
1166 p
= proc
-> gen_proc
;
1168 signature
= jvm_get_procedure_signature(proc
-> type
);
1169 jvm_generate_function_header(p
, "public static", proc
-> name
, signature
);
1171 /* Выделение регистров под параметры и переменные */
1172 oberon_object_t
* var
= proc
-> type
-> decl
;
1175 jvm_init_local_object(p
, var
);
1179 /* Копирование статических/открытых массивов и записей */
1180 var
= proc
-> type
-> decl
;
1183 if(var
-> class == OBERON_CLASS_PARAM
|| var
-> class == OBERON_CLASS_VAR_PARAM
)
1185 if(var
-> type
-> class == OBERON_TYPE_ARRAY
)
1187 jvm_generate_array_duplicate_and_replace(p
, var
-> gen_var
, var
-> type
);
1189 else if(var
-> type
-> class == OBERON_TYPE_RECORD
)
1191 jvm_generate_record_duplicate_and_replace(p
, var
-> gen_var
, var
-> type
);
1199 oberon_generate_end_proc(oberon_context_t
* ctx
)
1204 m
= ctx
-> mod
-> gen_mod
;
1205 p
= m
-> class -> p
;
1207 jvm_generate_function_end(p
);
1211 jvm_generate_call_proc(gen_proc_t
* p
, oberon_expr_t
* desig
)
1213 assert(desig
-> is_item
);
1214 assert(desig
-> item
.mode
== MODE_CALL
);
1216 oberon_object_t
* proc
;
1220 proc
= desig
-> item
.var
;
1221 fullname
= jvm_get_field_full_name(proc
);
1222 signature
= jvm_get_procedure_signature(proc
-> type
);
1224 if(proc
-> class != OBERON_CLASS_PROC
)
1226 /* Загружаем указатель на процедуру */
1227 jvm_generate_load(p
, proc
-> type
, proc
-> gen_var
);
1231 int result_cells
= jvm_cell_size_for_type(proc
-> type
-> base
);
1233 int num
= desig
-> item
.num_args
;
1234 oberon_expr_t
* arg
= desig
-> item
.args
;
1235 for(int i
= 0; i
< num
; i
++)
1237 args_cells
+= jvm_cell_size_for_type(arg
-> result
);
1242 if(proc
-> class == OBERON_CLASS_PROC
)
1244 /* Обычная статическая процедура */
1245 jvm_generate(p
, args_cells
, result_cells
, "invokestatic %s%s", fullname
, signature
);
1249 /* Процедура-переменная */
1250 char * class = jvm_get_class_full_name(proc
-> type
);
1251 jvm_generate(p
, 1 + args_cells
, result_cells
, "invokevirtual %s/invoke%s", class, signature
);
1256 oberon_generate_call_proc(oberon_context_t
* ctx
, oberon_expr_t
* desig
)
1258 jvm_generate_call_proc(ctx
-> mod
-> gen_mod
-> class -> p
, desig
);
1262 oberon_generate_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1269 m
= ctx
-> mod
-> gen_mod
;
1270 p
= m
-> class -> p
;
1275 prefix
= jvm_get_prefix(expr
-> result
);
1276 cell_size
= jvm_cell_size_for_type(expr
-> result
);
1277 jvm_generate(p
, cell_size
, 0, "%creturn", prefix
);
1281 jvm_generate(p
, 0, 0, "return");
1286 jvm_generate_expr_new_pointer(gen_proc_t
* p
, oberon_type_t
* type
, int num
, oberon_expr_t
* arg
)
1288 assert(type
-> class == OBERON_TYPE_POINTER
);
1290 for(int i
= 0; i
< num
; i
++)
1296 jvm_generate_new(p
, type
-> base
, num
);
1300 jvm_generate_cast_type(gen_proc_t
* p
, oberon_type_t
* from
, oberon_type_t
* to
)
1302 char prefix
= jvm_get_prefix(from
);
1303 char postfix
= jvm_get_postfix(to
);
1304 int from_cell_size
= jvm_cell_size_for_type(from
);
1305 int to_cell_size
= jvm_cell_size_for_type(to
);
1307 jvm_generate_comment(p
, "cast type class from %i(%i) to %i(%i)", from
-> class, from
-> size
, to
-> class, to
-> size
);
1309 if(prefix
== postfix
)
1314 jvm_generate(p
, from_cell_size
, to_cell_size
, "%c2%c", prefix
, postfix
);
1318 push_item(gen_proc_t
* p
, oberon_item_t
* item
)
1320 switch(item
-> mode
)
1323 if(item
-> var
-> class == OBERON_CLASS_PROC
)
1325 jvm_generate_push_procedure_pointer(p
, item
-> var
);
1329 jvm_generate_load(p
, item
-> result
, item
-> var
-> gen_var
);
1333 jvm_generate_push_int_size(p
, item
-> integer
, item
-> result
-> size
);
1336 jvm_generate_push_int_size(p
, item
-> boolean
, item
-> result
-> size
);
1339 jvm_generate_call_proc(p
, (oberon_expr_t
*) item
);
1343 char postfix
= jvm_get_postfix(item
-> result
);
1344 int cell_size
= jvm_cell_size_for_postfix(postfix
);
1345 assert(item
-> parent
-> is_item
);
1346 push_item(p
, (oberon_item_t
*) item
-> parent
);
1347 push_expr(p
, item
-> args
);
1348 jvm_generate(p
, 1 + 1, cell_size
, "%caload", postfix
);
1351 assert(item
-> parent
-> is_item
);
1352 push_item(p
, (oberon_item_t
*) item
-> parent
);
1353 jvm_generate_load(p
, item
-> result
, item
-> var
-> gen_var
);
1356 /* Все объекты в jvm представляются как указатели */
1357 assert(item
-> parent
-> is_item
);
1358 push_item(p
, (oberon_item_t
*) item
-> parent
);
1361 jvm_generate(p
, 0, 1, "aconst_null");
1364 jvm_generate_expr_new_pointer(p
, item
-> result
, item
-> num_args
, item
-> args
);
1367 jvm_generate_push_float(p
, item
-> real
, item
-> result
-> size
);
1370 push_expr(p
, item
-> parent
);
1371 jvm_generate_cast_type(p
, item
-> parent
-> result
, item
-> result
);
1374 gen_error("push_item: unk mode %i", item
-> mode
);
1380 jvm_generate_logical_not(gen_proc_t
* p
)
1382 int label_done
= jvm_new_label_id(p
);
1383 int label_false
= jvm_new_label_id(p
);
1384 char * label_name_done
= jvm_get_label_name(label_done
);
1385 char * label_name_false
= jvm_get_label_name(label_false
);
1387 jvm_generate(p
, 1, 0, "ifne %s", label_name_false
);
1388 jvm_generate(p
, 0, 1, "iconst_1");
1389 jvm_generate(p
, 0, 0, "goto %s", label_name_done
);
1390 jvm_generate_label(p
, label_false
);
1391 jvm_generate(p
, 0, 1, "iconst_0");
1392 jvm_generate_label(p
, label_done
);
1396 jvm_generate_abs(gen_proc_t
* p
, char prefix
)
1398 char t
= jvm_get_type_of_prefix(prefix
);
1399 int cell_size
= jvm_cell_size_for_postfix(prefix
);
1400 jvm_generate(p
, cell_size
, cell_size
, "invokestatic java/lang/Math/abs(%c)%c", t
, t
);
1404 jvm_generate_compare_op(gen_proc_t
* p
, char prefix
, int op
)
1406 int label_true
= jvm_new_label_id(p
);
1407 int label_done
= jvm_new_label_id(p
);
1408 char * label_name_true
= jvm_get_label_name(label_true
);
1409 char * label_name_done
= jvm_get_label_name(label_done
);
1410 int cell_size
= 2 * jvm_cell_size_for_postfix(prefix
);
1412 assert(prefix
== 'i' || prefix
== 'a');
1414 const char * cmpop
= "";
1436 gen_error("jvm_generate_compare_op: wat");
1440 jvm_generate(p
, cell_size
, 0, "if_%ccmp%s %s", prefix
, cmpop
, label_name_true
);
1441 jvm_generate(p
, 0, 1, "iconst_0");
1442 jvm_generate(p
, 0, 0, "goto %s", label_name_done
);
1443 jvm_generate_label(p
, label_true
);
1444 jvm_generate(p
, 0, 1, "iconst_1");
1445 jvm_generate_label(p
, label_done
);
1449 jvm_generate_operator(gen_proc_t
* p
, char prefix
, int op
)
1451 int cell_size
= jvm_cell_size_for_postfix(prefix
);
1454 case OP_UNARY_MINUS
:
1455 jvm_generate(p
, cell_size
, cell_size
, "%cneg", prefix
);
1457 case OP_BITWISE_NOT
:
1458 jvm_generate_push_int(p
, -1);
1459 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cxor", prefix
);
1462 jvm_generate_logical_not(p
);
1465 jvm_generate_abs(p
, prefix
);
1469 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cadd", prefix
);
1472 jvm_generate(p
, 2 * cell_size
, cell_size
, "%csub", prefix
);
1475 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cmul", prefix
);
1478 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cdiv", prefix
);
1481 jvm_generate(p
, 2 * cell_size
, cell_size
, "%crem", prefix
);
1483 case OP_BITWISE_AND
:
1484 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cand", prefix
);
1486 case OP_BITWISE_XOR
:
1487 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cxor", prefix
);
1490 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cor", prefix
);
1499 jvm_generate_compare_op(p
, prefix
, op
);
1502 gen_error("jvm_generate_operator: unk op %i", op
);
1508 jvm_generate_logical_or(gen_proc_t
* p
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1510 int label_calc_b
= jvm_new_label_id(p
);
1511 int label_done
= jvm_new_label_id(p
);
1512 char * label_name_calc_b
= jvm_get_label_name(label_calc_b
);
1513 char * label_name_done
= jvm_get_label_name(label_done
);
1515 /* a OR b -- если a, то TRUE, иначе b */
1518 jvm_generate(p
, 1, 0, "ifne %s", label_name_calc_b
);
1519 jvm_generate(p
, 0, 1, "iconst_1");
1520 jvm_generate(p
, 0, 0, "goto %s", label_name_done
);
1521 jvm_generate_label(p
, label_calc_b
);
1523 jvm_generate_label(p
, label_done
);
1527 jvm_generate_logical_and(gen_proc_t
* p
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1529 int label_false
= jvm_new_label_id(p
);
1530 int label_done
= jvm_new_label_id(p
);
1531 char * label_name_false
= jvm_get_label_name(label_false
);
1532 char * label_name_done
= jvm_get_label_name(label_done
);
1534 /* a AND b -- если a, то b, иначе FALSE */
1537 jvm_generate(p
, 1, 0, "ifne %s", label_name_false
);
1539 jvm_generate(p
, 0, 0, "goto %s", label_name_done
);
1540 jvm_generate_label(p
, label_false
);
1541 jvm_generate(p
, 0, 1, "iconst_0");
1542 jvm_generate_label(p
, label_done
);
1546 push_operator(gen_proc_t
* p
, oberon_oper_t
* oper
)
1548 char prefix
= jvm_get_prefix(oper
-> result
);
1549 int op
= oper
-> op
;
1552 case OP_UNARY_MINUS
:
1553 case OP_BITWISE_NOT
:
1556 push_expr(p
, oper
-> left
);
1557 jvm_generate_operator(p
, prefix
, op
);
1565 case OP_BITWISE_AND
:
1566 case OP_BITWISE_XOR
:
1575 push_expr(p
, oper
-> left
);
1576 push_expr(p
, oper
-> right
);
1577 jvm_generate_operator(p
, prefix
, op
);
1581 jvm_generate_logical_or(p
, oper
-> left
, oper
-> right
);
1584 jvm_generate_logical_and(p
, oper
-> left
, oper
-> right
);
1587 gen_error("push_oper: unk op %i", op
);
1593 push_expr(gen_proc_t
* p
, oberon_expr_t
* expr
)
1597 push_item(p
, (oberon_item_t
*) expr
);
1601 push_operator(p
, (oberon_oper_t
*) expr
);
1606 store_expr(gen_proc_t
* p
, oberon_expr_t
* dst
, oberon_expr_t
* src
)
1608 assert(dst
-> is_item
);
1609 oberon_item_t
* item
= (oberon_item_t
*) dst
;
1611 if(dst
-> result
-> class == OBERON_TYPE_ARRAY
1612 || src
-> result
-> class == OBERON_TYPE_ARRAY
)
1616 jvm_generate_copy_array(p
, dst
-> result
);
1618 else if(dst
-> result
-> class == OBERON_TYPE_RECORD
1619 || src
-> result
-> class == OBERON_TYPE_RECORD
)
1623 jvm_generate_copy_record(p
, dst
-> result
);
1625 else switch(item
-> mode
)
1629 jvm_generate_store(p
, src
-> result
, item
-> var
-> gen_var
);
1633 char postfix
= jvm_get_postfix(src
-> result
);
1634 int cell_size
= jvm_cell_size_for_postfix(postfix
);
1635 assert(item
-> parent
-> is_item
);
1636 push_item(p
, (oberon_item_t
*) item
-> parent
);
1637 push_expr(p
, item
-> args
);
1639 jvm_generate(p
, 1 + 1 + cell_size
, 0, "%castore", postfix
);
1642 assert(item
-> parent
-> is_item
);
1643 push_item(p
, (oberon_item_t
*) item
-> parent
);
1645 jvm_generate_store(p
, src
-> result
, item
-> var
-> gen_var
);
1648 gen_error("store_expr: unk mode %i", item
-> mode
);
1654 oberon_generate_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
1659 m
= ctx
-> mod
-> gen_mod
;
1660 p
= m
-> class -> p
;
1662 store_expr(p
, dst
, src
);
1666 oberon_generate_code(oberon_context_t
* ctx
)
1668 printf("generate code\n");
1672 oberon_generator_dump(oberon_context_t
* ctx
, char * path
)
1674 printf("jit: dump code\n");
1678 oberon_generator_get_procedure(oberon_context_t
* ctx
, const char * name
)
1680 printf("jit: get pointer to procedure %s\n", name
);
1685 oberon_generator_get_var(oberon_context_t
* ctx
, const char * name
)
1687 printf("jit: get pointer to var %s\n", name
);