DEADSOFTWARE

Добавлено расширение типа
[dsw-obn.git] / src / backends / jvm / generator-jvm.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <string.h>
7 #include <assert.h>
9 #include <gc.h>
11 #include "../../../include/oberon.h"
12 #include "../../oberon-internals.h"
13 #include "generator-jvm.h"
14 #include "generator-jvm-basic.h"
16 // ==========================================
17 // ==========================================
18 // ==========================================
20 gen_proc_t *
21 jvm_create_proc(struct gen_class * class)
22 {
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);
32 p -> rf = rf;
33 p -> stack = stack;
34 p -> class = class;
35 p -> label_id = 0;
37 return p;
38 }
40 void
41 jvm_generate_function_header(gen_proc_t * p, char * access, char * name, char * signature)
42 {
43 struct gen_class * class;
44 class = p -> class;
46 /* Делаем процедуру текущей в этом классе */
47 assert(class -> p == NULL);
48 class -> p = p;
50 fprintf(class -> fp, ".method %s %s%s\n", access, name, signature);
51 fprintf(class -> fp, " start:\n");
52 }
54 void
55 jvm_generate_function_end(gen_proc_t * p)
56 {
57 struct gen_class * class;
58 class = p -> class;
60 assert(class -> p);
61 class -> p = NULL;
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");
72 }
74 struct gen_class *
75 jvm_create_class(char * full_name)
76 {
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");
83 assert(class -> fp);
85 return class;
86 }
88 static void
89 jvm_destroy_class(struct gen_class * class)
90 {
91 assert(class -> p == NULL);
92 fclose(class -> fp);
93 }
95 static void
96 jvm_stack_push(gen_proc_t * p, unsigned size)
97 {
98 p -> stack -> pointer += size;
99 if(p -> stack -> pointer > p -> stack -> max_pointer)
101 p -> stack -> max_pointer = p -> stack -> pointer;
105 static void
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);
116 static void
117 jvm_generate(gen_proc_t * p, unsigned get, unsigned push, char * format, ...)
119 va_list ptr;
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);
128 va_end(ptr);
131 static void
132 jvm_generate_comment(gen_proc_t * p, char * format, ...)
134 va_list ptr;
135 va_start(ptr, format);
137 fprintf(p -> class -> fp, " ;;;; ");
138 vfprintf(p -> class -> fp, format, ptr);
139 fprintf(p -> class -> fp, "\n");
141 va_end(ptr);
144 static void
145 jvm_generate_push_int(gen_proc_t * p, int64_t i)
147 if(i == -1)
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);
167 else
169 jvm_generate(p, 0, 2, "ldc2 %li", i);
173 static void
174 jvm_generate_push_int_size(gen_proc_t * p, int64_t i, int size)
176 int pushed_cell = 1;
178 if(i == -1)
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);
198 else
200 pushed_cell = 2;
201 jvm_generate(p, 0, 2, "ldc2 %li", i);
204 assert(size <= 8);
205 if(size > 4 && pushed_cell == 1)
207 jvm_generate(p, pushed_cell, 2, "i2l");
209 else if(size <= 4)
211 if(pushed_cell > 1)
213 jvm_generate(p, 2, 1, "l2i");
216 if(size == 2)
218 jvm_generate(p, 1, 1, "i2s");
220 else if(size == 1)
222 jvm_generate(p, 1, 1, "i2b");
227 static void
228 jvm_generate_push_float(gen_proc_t * p, double f, int size)
230 if(size <= 4)
232 if(f == 0.0)
234 jvm_generate(p, 0, 1, "fconst_0");
236 if(f == 1.0)
238 jvm_generate(p, 0, 1, "fconst_1");
240 if(f == 2.0)
242 jvm_generate(p, 0, 1, "fconst_2");
244 else
246 jvm_generate(p, 0, 1, "ldc %lf", f);
249 else
251 if(f == 0.0)
253 jvm_generate(p, 0, 2, "dconst_0");
255 if(f == 1.0)
257 jvm_generate(p, 0, 2, "dconst_1");
259 else
261 jvm_generate(p, 0, 2, "ldc2 %lf", f);
266 static gen_var_t *
267 oberon_generator_new_var()
269 gen_var_t * v = GC_MALLOC(sizeof *v);
270 memset(v, 0, sizeof *v);
271 return v;
274 static void
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);
285 static void
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);
296 static void
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);
303 static void
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);
311 static int
312 jvm_new_label_id(gen_proc_t * p)
314 int label_id = p -> label_id;
315 p -> label_id += 1;
316 return label_id;
319 static char *
320 jvm_get_label_name(int label_id)
322 return new_string("L%i", label_id);
325 static void
326 jvm_generate_label(gen_proc_t * p, int label_id)
328 jvm_generate(p, 0, 0, "L%i:", label_id);
331 static void
332 jvm_generate_load(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * src)
334 char prefix;
335 int cell_size;
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);
344 break;
345 case JVM_STORAGE_STATIC:
346 jvm_generate(p, 0, cell_size, "getstatic %s %s", src -> full_name, src -> desc);
347 break;
348 case JVM_STORAGE_FIELD:
349 jvm_generate(p, 1, cell_size, "getfield %s %s", src -> full_name, src -> desc);
350 break;
351 default:
352 gen_error("jvm_generate_load: unknow storage type %i", src -> storage);
353 break;
357 static void
358 jvm_generate_store(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * dst)
360 char prefix;
361 int cell_size;
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);
370 break;
371 case JVM_STORAGE_STATIC:
372 jvm_generate(p, cell_size, 0, "putstatic %s %s", dst -> full_name, dst -> desc);
373 break;
374 case JVM_STORAGE_FIELD:
375 jvm_generate(p, 1 + cell_size, 0, "putfield %s %s", dst -> full_name, dst -> desc);
376 break;
377 default:
378 gen_error("jvm_generate_store: unknow storage type %i", dst -> storage);
379 break;
383 static void
384 jvm_generate_new(gen_proc_t * p, oberon_type_t * type, int num);
386 /*
387 * Генерирует код для инициализации массива со статическим базовым типом
388 * ( aref -- )
389 */
391 static void
392 jvm_generate_array_initialization(gen_proc_t * p, oberon_type_t * arr)
394 int dim = 0;
395 oberon_type_t * base = arr;
396 while(base -> class == OBERON_TYPE_ARRAY)
398 dim += 1;
399 base = base -> base;
402 if(base -> class != OBERON_TYPE_RECORD)
404 jvm_generate(p, 1, 0, "pop");
405 return;
408 struct {
409 gen_var_t * index;
410 gen_var_t * length;
411 int start;
412 int end;
413 } loop[dim];
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);
419 /*
420 * Входящие параметры заграблены.
421 * Теперь генерируем эквивалентный код:
422 * int i = 0;
423 * int len = dst.length
424 * while(i < len)
425 * {
426 * ...
427 * {
428 * dst[i, ...] = new record;
429 * }
430 * ...
431 * i += 1;
432 * }
433 * Где "..." такой же код (начало и конец) для следующей размерности.
434 */
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);
479 static void
480 jvm_generate_new(gen_proc_t * p, oberon_type_t * type, int num)
482 int dim;
483 char * cname;
484 char * desc;
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");
495 break;
496 case OBERON_TYPE_RECORD:
497 assert(num == 0);
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);
502 break;
503 case OBERON_TYPE_ARRAY:
504 dim = 0;
505 base = type;
506 desc = jvm_get_descriptor(type);
507 while(base -> class == OBERON_TYPE_ARRAY)
509 if(num > 0)
511 assert(base -> size == 0);
512 num -= 1;
514 else
516 assert(base -> size > 0);
517 jvm_generate_push_int(p, base -> size);
519 dim += 1;
520 base = base -> base;
523 assert(num == 0);
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);
527 break;
528 default:
529 gen_error("jvm_generate_new_static: unk type class %i", type -> class);
530 break;
534 /*
535 * Генерирует код для копирования полей из первой записи во вторую.
536 * ( aref_dst aref_src -- )
537 * dst := src;
538 */
540 static void
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);
549 /*
550 * Генерирует кода для копирования массивов.
551 * ( aref_dst aref_src -- )
552 * dst := src;
553 */
555 static void
556 jvm_generate_copy_array(gen_proc_t * p, oberon_type_t * arr)
558 int dim = 0;
559 oberon_type_t * base = arr;
560 while(base -> class == OBERON_TYPE_ARRAY)
562 dim += 1;
563 base = base -> base;
566 struct {
567 gen_var_t * index;
568 gen_var_t * length;
569 int start;
570 int end;
571 } loop[dim];
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);
581 /*
582 * Входящие параметры заграблены.
583 * Теперь генерируем эквивалентный код:
584 * int i = 0;
585 * int len = dst.length
586 * while(i < len)
587 * {
588 * ...
589 * {
590 * copy from src[i, ...] to dst[i, ...];
591 * }
592 * ...
593 * i += 1;
594 * }
595 * Где "..." такой же код (начало и конец) для следующей размерности.
596 */
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);
644 else
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);
680 static void
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 // ==========================================
692 void
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;
701 void
702 oberon_generator_destroy_context(oberon_context_t * ctx)
706 static void
707 oberon_generate_procedure_class(oberon_type_t * proc)
709 FILE * fp;
710 char * cname;
711 char * signature;
712 struct gen_class * class;
714 cname = jvm_get_class_full_name(proc);
715 class = jvm_create_class(cname);
716 fp = class -> fp;
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);
736 static void
737 oberon_generate_procedure_pointer_class(oberon_object_t * proc)
739 FILE * fp;
740 char * cname;
741 char * abscname;
742 char * absdesc;
743 char * signature;
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);
750 fp = class -> fp;
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);
768 gen_proc_t * p;
769 gen_var_t * this_v;
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);
777 gen_var_t * v;
778 int use_size = 0;
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);
792 arg = arg -> next;
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");
804 else
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);
815 static void
816 oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec)
818 char * cname;
819 struct gen_class * class;
821 /* Устанавливаем новоый id */
822 rec -> gen_type -> rec_id = m -> rec_id;
823 m -> rec_id += 1;
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);
831 if(rec -> base == NULL)
833 fprintf(class -> fp, ".super java/lang/Object\n\n");
835 else
837 class -> base = rec -> base -> gen_type -> class;
838 fprintf(class -> fp, ".super %s\n\n", class -> base -> full_name);
841 rec -> gen_type -> class = class;
844 void
845 oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type)
847 gen_type_t * t = GC_MALLOC(sizeof *t);
848 memset(t, 0, sizeof *t);
849 type -> gen_type = t;
851 switch(type -> class)
853 case OBERON_TYPE_VOID:
854 case OBERON_TYPE_INTEGER:
855 case OBERON_TYPE_BOOLEAN:
856 case OBERON_TYPE_ARRAY:
857 case OBERON_TYPE_REAL:
858 break;
859 case OBERON_TYPE_RECORD:
861 gen_module_t * m;
862 m = type -> module -> gen_mod;
863 oberon_generate_record_class(m, type);
864 break;
865 case OBERON_TYPE_PROCEDURE:
866 oberon_generate_procedure_class(type);
867 break;
868 case OBERON_TYPE_POINTER:
869 break;
870 default:
871 gen_error("oberon_generator_init_type: unk calss %i", type -> class);
872 break;
876 void
877 oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
879 struct gen_class * class;
880 class = rec -> gen_type -> class;
882 int num = rec -> num_decl;
883 oberon_object_t * field = rec -> decl;
884 for(int i = 0; i < num; i++)
886 gen_var_t * v = field -> gen_var;
887 char * name = field -> name;
888 char * desc = jvm_get_descriptor(field -> type);
889 jvm_generate_and_init_field(v, class, "public", name, desc);
890 field = field -> next;
893 /* Стандартный конструктор класса */
894 /* Инициализирует внутренние статические записи и массивы */
895 gen_proc_t * p = jvm_create_proc(class);
896 jvm_generate_function_header(p, "public", "<init>", "()V");
897 jvm_alloc_register_untyped(p -> rf, false);
898 jvm_generate(p, 0, 1, "aload_0");
899 if(class -> base)
901 jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", class -> base -> full_name);
903 else
905 jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
907 num = rec -> num_decl;
908 field = rec -> decl;
909 for(int i = 0; i < num; i++)
911 if(field -> type -> class == OBERON_TYPE_RECORD
912 || field -> type -> class == OBERON_TYPE_ARRAY)
914 jvm_generate(p, 0, 1, "aload_0");
915 jvm_generate_new(p, field -> type, 0);
916 jvm_generate_store(p, field -> type, field -> gen_var);
918 field = field -> next;
920 jvm_generate(p, 0, 0, "return");
921 jvm_generate_function_end(p);
923 /* Метод для копирования полей класса */
924 /* reg0 == src -> reg1 == dst */
925 p = jvm_create_proc(class);
926 gen_var_t * copy_dst = oberon_generator_new_var();
927 gen_var_t * copy_src = oberon_generator_new_var();
928 char * desc = jvm_get_descriptor(rec);
929 char * signature = new_string("(%s%s)V", desc, desc);
930 jvm_generate_function_header(p, "public static", "$COPY$", signature);
931 jvm_generate_and_init_named_local_var(copy_dst, p, false, "dst", desc);
932 jvm_generate_and_init_named_local_var(copy_src, p, false, "src", desc);
933 num = rec -> num_decl;
934 field = rec -> decl;
935 for(int i = 0; i < num; i++)
937 if(field -> type -> class == OBERON_TYPE_RECORD)
939 jvm_generate_load(p, rec, copy_dst);
940 jvm_generate_load(p, field -> type, field -> gen_var);
941 jvm_generate_load(p, rec, copy_src);
942 jvm_generate_load(p, field -> type, field -> gen_var);
943 jvm_generate_copy_record(p, field -> type);
945 else if(field -> type -> class == OBERON_TYPE_ARRAY)
947 jvm_generate_load(p, rec, copy_dst);
948 jvm_generate_load(p, field -> type, field -> gen_var);
949 jvm_generate_load(p, rec, copy_src);
950 jvm_generate_load(p, field -> type, field -> gen_var);
951 jvm_generate_copy_array(p, field -> type);
953 else
955 jvm_generate_load(p, rec, copy_dst);
956 jvm_generate_load(p, rec, copy_src);
957 jvm_generate_load(p, field -> type, field -> gen_var);
958 jvm_generate_store(p, field -> type, field -> gen_var);
960 field = field -> next;
962 jvm_generate(p, 0, 0, "return");
963 jvm_generate_function_end(p);
965 jvm_destroy_class(class);
968 void
969 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
971 gen_var_t * v = oberon_generator_new_var();
972 var -> gen_var = v;
974 gen_module_t * m;
975 m = ctx -> mod -> gen_mod;
977 struct gen_class * class;
978 class = m -> class;
980 char * desc;
981 switch(var -> class)
983 case OBERON_CLASS_VAR_PARAM:
984 gen_error("generator: VAR-parameters not implemented");
985 break;
986 case OBERON_CLASS_PARAM:
987 /* Заполняется при генерации функции */
988 /* смотри jvm_init_local_object() */
989 break;
990 case OBERON_CLASS_FIELD:
991 /* Заполняются при инициализации структуры */
992 /* смотри oberon_generator_init_record() */
993 break;
994 case OBERON_CLASS_VAR:
995 /* Локальные заполняются при генерации функции */
996 /* смотри jvm_init_local_object() */
997 if(var -> local == 0)
999 desc = jvm_get_descriptor(var -> type);
1000 jvm_generate_and_init_global_var(v, class, "public", var -> name, desc);
1002 break;
1003 default:
1004 gen_error("oberon_generator_init_var: unk var class %i", var -> class);
1005 break;
1009 void
1010 oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
1012 struct gen_class * class = proc -> module -> gen_mod -> class;
1013 proc -> gen_proc = jvm_create_proc(class);
1015 if(proc -> local)
1017 gen_error("generator: local procedures not implemented");
1020 oberon_generate_procedure_pointer_class(proc);
1023 void
1024 oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod)
1026 gen_module_t * m = GC_MALLOC(sizeof *m);
1027 memset(m, 0, sizeof *m);
1028 mod -> gen_mod = m;
1030 struct gen_class * class;
1031 class = jvm_create_class(mod -> name);
1033 fprintf(class -> fp, ".source %s\n", mod -> name);
1034 fprintf(class -> fp, ".class %s\n", mod -> name);
1035 fprintf(class -> fp, ".super java/lang/Object\n\n");
1037 m -> class = class;
1039 ctx -> gen_context -> current_m = m;
1042 void
1043 oberon_generator_fini_module(oberon_module_t * mod)
1045 jvm_destroy_class(mod -> gen_mod -> class);
1048 static void
1049 push_expr(gen_proc_t * p, oberon_expr_t * expr);
1051 void
1052 oberon_generate_begin_module(oberon_context_t * ctx)
1054 struct gen_class * class = ctx -> mod -> gen_mod -> class;
1055 gen_proc_t * p = jvm_create_proc(class);
1057 jvm_generate_function_header(p, "public", "<init>", "()V");
1058 jvm_alloc_register_untyped(p -> rf, false);
1059 jvm_generate(p, 0, 1, "aload_0");
1060 jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
1062 /* Инициализация переменных объявленных в модуле */
1063 oberon_object_t * x = ctx -> mod -> decl -> list -> next;
1064 while(x != NULL)
1066 if(x -> class == OBERON_CLASS_VAR)
1068 if(x -> type -> class == OBERON_TYPE_ARRAY
1069 || x -> type -> class == OBERON_TYPE_RECORD)
1071 jvm_generate_new(p, x -> type, 0);
1072 jvm_generate_store(p, x -> type, x -> gen_var);
1075 else if(x -> class == OBERON_CLASS_PROC)
1077 char * cname = jvm_get_field_full_name_safe(x);
1078 jvm_generate(p, 0, 1, "new %s", cname);
1079 jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", cname);
1082 x = x -> next;
1086 void
1087 oberon_generate_end_module(oberon_context_t * ctx)
1089 struct gen_class * class = ctx -> mod -> gen_mod -> class;
1090 gen_proc_t * p = class -> p;
1092 jvm_generate(p, 0, 0, "return");
1093 jvm_generate_function_end(class -> p);
1096 static void
1097 jvm_init_local_object(gen_proc_t * p, oberon_object_t * x)
1099 gen_var_t * v;
1100 bool wide;
1101 char * desc;
1102 struct gen_class * class;
1104 v = x -> gen_var;
1105 class = p -> class;
1106 wide = jvm_is_wide_type(x -> type);
1107 desc = jvm_get_descriptor(x -> type);
1109 // Убеждаемся что сейчас находимся в функции
1110 assert(class -> p);
1111 assert(x -> local);
1113 switch(x -> class)
1115 case OBERON_CLASS_VAR:
1116 case OBERON_CLASS_PARAM:
1117 jvm_generate_and_init_named_local_var(v, p, wide, x -> name, desc);
1118 break;
1119 default:
1120 gen_error("jvm_init_local_object: wat");
1121 break;
1125 /*
1126 * Генерирует код для получения размера измерения массива
1127 * Аналог Обероновского LEN(v, n); где n = 0 - первое измерение.
1128 * ( aref -- int )
1129 */
1131 static void
1132 jvm_generate_array_len(gen_proc_t * p, int dim)
1134 while(dim > 0)
1136 jvm_generate(p, 0, 1, "iconst_0");
1137 jvm_generate(p, 2, 1, "aaload");
1138 dim -= 1;
1140 jvm_generate(p, 1, 1, "arraylength");
1143 static void
1144 jvm_generate_array_duplicate_and_replace(gen_proc_t * p, gen_var_t * v, oberon_type_t * arr)
1146 int dim = 0;
1147 oberon_type_t * base = arr;
1148 while(base -> class == OBERON_TYPE_ARRAY)
1150 if(base -> size == 0)
1152 jvm_generate_load(p, arr, v);
1153 jvm_generate_array_len(p, dim);
1154 dim += 1;
1156 base = base -> base;
1159 jvm_generate_new(p, arr, dim);
1160 jvm_generate(p, 1, 2, "dup");
1161 jvm_generate_load(p, arr, v);
1162 jvm_generate_copy_array(p, arr);
1163 jvm_generate_store(p, arr, v);
1166 static void
1167 jvm_generate_record_duplicate_and_replace(gen_proc_t * p, gen_var_t * v, oberon_type_t * rec)
1169 jvm_generate_new(p, rec, 0);
1170 jvm_generate(p, 1, 2, "dup");
1171 jvm_generate_load(p, rec, v);
1172 jvm_generate_copy_record(p, rec);
1173 jvm_generate_store(p, rec, v);
1176 void
1177 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
1179 gen_proc_t * p;
1180 char * signature;
1182 p = proc -> gen_proc;
1184 signature = jvm_get_procedure_signature(proc -> type);
1185 jvm_generate_function_header(p, "public static", proc -> name, signature);
1187 /* Выделение регистров под параметры и переменные */
1188 oberon_object_t * var = proc -> type -> decl;
1189 while(var)
1191 jvm_init_local_object(p, var);
1192 var = var -> next;
1195 /* Копирование статических/открытых массивов и записей */
1196 var = proc -> type -> decl;
1197 while(var)
1199 if(var -> class == OBERON_CLASS_PARAM || var -> class == OBERON_CLASS_VAR_PARAM)
1201 if(var -> type -> class == OBERON_TYPE_ARRAY)
1203 jvm_generate_array_duplicate_and_replace(p, var -> gen_var, var -> type);
1205 else if(var -> type -> class == OBERON_TYPE_RECORD)
1206 {
1207 jvm_generate_record_duplicate_and_replace(p, var -> gen_var, var -> type);
1210 var = var -> next;
1211 }
1214 void
1215 oberon_generate_end_proc(oberon_context_t * ctx)
1217 gen_module_t * m;
1218 gen_proc_t * p;
1220 m = ctx -> mod -> gen_mod;
1221 p = m -> class -> p;
1223 jvm_generate_function_end(p);
1226 static void
1227 jvm_generate_call_proc(gen_proc_t * p, oberon_expr_t * desig)
1229 assert(desig -> is_item);
1230 assert(desig -> item.mode == MODE_CALL);
1232 oberon_object_t * proc;
1233 char * fullname;
1234 char * signature;
1236 proc = desig -> item.var;
1237 fullname = jvm_get_field_full_name(proc);
1238 signature = jvm_get_procedure_signature(proc -> type);
1240 if(proc -> class != OBERON_CLASS_PROC)
1242 /* Загружаем указатель на процедуру */
1243 jvm_generate_load(p, proc -> type, proc -> gen_var);
1246 int args_cells = 0;
1247 int result_cells = jvm_cell_size_for_type(proc -> type -> base);
1249 int num = desig -> item.num_args;
1250 oberon_expr_t * arg = desig -> item.args;
1251 for(int i = 0; i < num; i++)
1253 args_cells += jvm_cell_size_for_type(arg -> result);
1254 push_expr(p, arg);
1255 arg = arg -> next;
1258 if(proc -> class == OBERON_CLASS_PROC)
1260 /* Обычная статическая процедура */
1261 jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", fullname, signature);
1263 else
1265 /* Процедура-переменная */
1266 char * class = jvm_get_class_full_name(proc -> type);
1267 jvm_generate(p, 1 + args_cells, result_cells, "invokevirtual %s/invoke%s", class, signature);
1271 void
1272 oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
1274 jvm_generate_call_proc(ctx -> mod -> gen_mod -> class -> p, desig);
1277 void
1278 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
1280 gen_module_t * m;
1281 gen_proc_t * p;
1282 char prefix;
1283 int cell_size;
1285 m = ctx -> mod -> gen_mod;
1286 p = m -> class -> p;
1288 if(expr)
1290 push_expr(p, expr);
1291 prefix = jvm_get_prefix(expr -> result);
1292 cell_size = jvm_cell_size_for_type(expr -> result);
1293 jvm_generate(p, cell_size, 0, "%creturn", prefix);
1295 else
1297 jvm_generate(p, 0, 0, "return");
1301 static void
1302 jvm_generate_expr_new_pointer(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
1304 assert(type -> class == OBERON_TYPE_POINTER);
1306 for(int i = 0; i < num; i++)
1308 push_expr(p, arg);
1309 arg = arg -> next;
1312 jvm_generate_new(p, type -> base, num);
1315 static void
1316 jvm_generate_cast_type(gen_proc_t * p, oberon_type_t * from, oberon_type_t * to)
1318 char prefix = jvm_get_prefix(from);
1319 char postfix = jvm_get_postfix(to);
1320 int from_cell_size = jvm_cell_size_for_type(from);
1321 int to_cell_size = jvm_cell_size_for_type(to);
1323 jvm_generate_comment(p, "cast type class from %i(%i) to %i(%i)", from -> class, from -> size, to -> class, to -> size);
1325 if(prefix == postfix)
1327 return;
1330 jvm_generate(p, from_cell_size, to_cell_size, "%c2%c", prefix, postfix);
1333 static void
1334 push_item(gen_proc_t * p, oberon_item_t * item)
1336 switch(item -> mode)
1338 case MODE_VAR:
1339 if(item -> var -> class == OBERON_CLASS_PROC)
1341 jvm_generate_push_procedure_pointer(p, item -> var);
1343 else
1345 jvm_generate_load(p, item -> result, item -> var -> gen_var);
1347 break;
1348 case MODE_INTEGER:
1349 jvm_generate_push_int_size(p, item -> integer, item -> result -> size);
1350 break;
1351 case MODE_BOOLEAN:
1352 jvm_generate_push_int_size(p, item -> boolean, item -> result -> size);
1353 break;
1354 case MODE_CALL:
1355 jvm_generate_call_proc(p, (oberon_expr_t *) item);
1356 break;
1357 case MODE_INDEX:
1359 char postfix = jvm_get_postfix(item -> result);
1360 int cell_size = jvm_cell_size_for_postfix(postfix);
1361 assert(item -> parent -> is_item);
1362 push_item(p, (oberon_item_t *) item -> parent);
1363 push_expr(p, item -> args);
1364 jvm_generate(p, 1 + 1, cell_size, "%caload", postfix);
1365 break;
1366 case MODE_FIELD:
1367 assert(item -> parent -> is_item);
1368 push_item(p, (oberon_item_t *) item -> parent);
1369 jvm_generate_load(p, item -> result, item -> var -> gen_var);
1370 break;
1371 case MODE_DEREF:
1372 /* Все объекты в jvm представляются как указатели */
1373 assert(item -> parent -> is_item);
1374 push_item(p, (oberon_item_t *) item -> parent);
1375 break;
1376 case MODE_NIL:
1377 jvm_generate(p, 0, 1, "aconst_null");
1378 break;
1379 case MODE_NEW:
1380 jvm_generate_expr_new_pointer(p, item -> result, item -> num_args, item -> args);
1381 break;
1382 case MODE_REAL:
1383 jvm_generate_push_float(p, item -> real, item -> result -> size);
1384 break;
1385 case MODE_CAST:
1386 push_expr(p, item -> parent);
1387 jvm_generate_cast_type(p, item -> parent -> result, item -> result);
1388 break;
1389 default:
1390 gen_error("push_item: unk mode %i", item -> mode);
1391 break;
1395 static void
1396 jvm_generate_logical_not(gen_proc_t * p)
1398 int label_done = jvm_new_label_id(p);
1399 int label_false = jvm_new_label_id(p);
1400 char * label_name_done = jvm_get_label_name(label_done);
1401 char * label_name_false = jvm_get_label_name(label_false);
1403 jvm_generate(p, 1, 0, "ifne %s", label_name_false);
1404 jvm_generate(p, 0, 1, "iconst_1");
1405 jvm_generate(p, 0, 0, "goto %s", label_name_done);
1406 jvm_generate_label(p, label_false);
1407 jvm_generate(p, 0, 1, "iconst_0");
1408 jvm_generate_label(p, label_done);
1411 static void
1412 jvm_generate_abs(gen_proc_t * p, char prefix)
1414 char t = jvm_get_type_of_prefix(prefix);
1415 int cell_size = jvm_cell_size_for_postfix(prefix);
1416 jvm_generate(p, cell_size, cell_size, "invokestatic java/lang/Math/abs(%c)%c", t, t);
1419 static void
1420 jvm_generate_compare_op(gen_proc_t * p, char prefix, int op)
1422 int label_true = jvm_new_label_id(p);
1423 int label_done = jvm_new_label_id(p);
1424 char * label_name_true = jvm_get_label_name(label_true);
1425 char * label_name_done = jvm_get_label_name(label_done);
1426 int cell_size = 2 * jvm_cell_size_for_postfix(prefix);
1428 assert(prefix == 'i' || prefix == 'a');
1430 const char * cmpop = "";
1431 switch(op)
1433 case OP_EQ:
1434 cmpop = "eq";
1435 break;
1436 case OP_NEQ:
1437 cmpop = "ne";
1438 break;
1439 case OP_LSS:
1440 cmpop = "lt";
1441 break;
1442 case OP_LEQ:
1443 cmpop = "le";
1444 break;
1445 case OP_GRT:
1446 cmpop = "gt";
1447 break;
1448 case OP_GEQ:
1449 cmpop = "ge";
1450 break;
1451 default:
1452 gen_error("jvm_generate_compare_op: wat");
1453 break;
1456 jvm_generate(p, cell_size, 0, "if_%ccmp%s %s", prefix, cmpop, label_name_true);
1457 jvm_generate(p, 0, 1, "iconst_0");
1458 jvm_generate(p, 0, 0, "goto %s", label_name_done);
1459 jvm_generate_label(p, label_true);
1460 jvm_generate(p, 0, 1, "iconst_1");
1461 jvm_generate_label(p, label_done);
1464 static void
1465 jvm_generate_operator(gen_proc_t * p, char prefix, int op)
1467 int cell_size = jvm_cell_size_for_postfix(prefix);
1468 switch(op)
1470 case OP_UNARY_MINUS:
1471 jvm_generate(p, cell_size, cell_size, "%cneg", prefix);
1472 break;
1473 case OP_BITWISE_NOT:
1474 jvm_generate_push_int(p, -1);
1475 jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
1476 break;
1477 case OP_LOGIC_NOT:
1478 jvm_generate_logical_not(p);
1479 break;
1480 case OP_ABS:
1481 jvm_generate_abs(p, prefix);
1482 break;
1484 case OP_ADD:
1485 jvm_generate(p, 2 * cell_size, cell_size, "%cadd", prefix);
1486 break;
1487 case OP_SUB:
1488 jvm_generate(p, 2 * cell_size, cell_size, "%csub", prefix);
1489 break;
1490 case OP_MUL:
1491 jvm_generate(p, 2 * cell_size, cell_size, "%cmul", prefix);
1492 break;
1493 case OP_DIV:
1494 jvm_generate(p, 2 * cell_size, cell_size, "%cdiv", prefix);
1495 break;
1496 case OP_MOD:
1497 jvm_generate(p, 2 * cell_size, cell_size, "%crem", prefix);
1498 break;
1499 case OP_BITWISE_AND:
1500 jvm_generate(p, 2 * cell_size, cell_size, "%cand", prefix);
1501 break;
1502 case OP_BITWISE_XOR:
1503 jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
1504 break;
1505 case OP_BITWISE_OR:
1506 jvm_generate(p, 2 * cell_size, cell_size, "%cor", prefix);
1507 break;
1509 case OP_EQ:
1510 case OP_NEQ:
1511 case OP_LSS:
1512 case OP_LEQ:
1513 case OP_GRT:
1514 case OP_GEQ:
1515 jvm_generate_compare_op(p, prefix, op);
1516 break;
1517 default:
1518 gen_error("jvm_generate_operator: unk op %i", op);
1519 break;
1520 }
1523 static void
1524 jvm_generate_logical_or(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
1526 int label_calc_b = jvm_new_label_id(p);
1527 int label_done = jvm_new_label_id(p);
1528 char * label_name_calc_b = jvm_get_label_name(label_calc_b);
1529 char * label_name_done = jvm_get_label_name(label_done);
1531 /* a OR b -- если a, то TRUE, иначе b */
1533 push_expr(p, a);
1534 jvm_generate(p, 1, 0, "ifne %s", label_name_calc_b);
1535 jvm_generate(p, 0, 1, "iconst_1");
1536 jvm_generate(p, 0, 0, "goto %s", label_name_done);
1537 jvm_generate_label(p, label_calc_b);
1538 push_expr(p, b);
1539 jvm_generate_label(p, label_done);
1542 static void
1543 jvm_generate_logical_and(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
1545 int label_false = jvm_new_label_id(p);
1546 int label_done = jvm_new_label_id(p);
1547 char * label_name_false = jvm_get_label_name(label_false);
1548 char * label_name_done = jvm_get_label_name(label_done);
1550 /* a AND b -- если a, то b, иначе FALSE */
1552 push_expr(p, a);
1553 jvm_generate(p, 1, 0, "ifne %s", label_name_false);
1554 push_expr(p, b);
1555 jvm_generate(p, 0, 0, "goto %s", label_name_done);
1556 jvm_generate_label(p, label_false);
1557 jvm_generate(p, 0, 1, "iconst_0");
1558 jvm_generate_label(p, label_done);
1561 static void
1562 push_operator(gen_proc_t * p, oberon_oper_t * oper)
1564 char prefix = jvm_get_prefix(oper -> result);
1565 int op = oper -> op;
1566 switch(op)
1568 case OP_UNARY_MINUS:
1569 case OP_BITWISE_NOT:
1570 case OP_LOGIC_NOT:
1571 case OP_ABS:
1572 push_expr(p, oper -> left);
1573 jvm_generate_operator(p, prefix, op);
1574 break;
1576 case OP_ADD:
1577 case OP_SUB:
1578 case OP_MUL:
1579 case OP_DIV:
1580 case OP_MOD:
1581 case OP_BITWISE_AND:
1582 case OP_BITWISE_XOR:
1583 case OP_BITWISE_OR:
1585 case OP_EQ:
1586 case OP_NEQ:
1587 case OP_LSS:
1588 case OP_LEQ:
1589 case OP_GRT:
1590 case OP_GEQ:
1591 push_expr(p, oper -> left);
1592 push_expr(p, oper -> right);
1593 jvm_generate_operator(p, prefix, op);
1594 break;
1596 case OP_LOGIC_OR:
1597 jvm_generate_logical_or(p, oper -> left, oper -> right);
1598 break;
1599 case OP_LOGIC_AND:
1600 jvm_generate_logical_and(p, oper -> left, oper -> right);
1601 break;
1602 default:
1603 gen_error("push_oper: unk op %i", op);
1604 break;
1608 static void
1609 push_expr(gen_proc_t * p, oberon_expr_t * expr)
1611 if(expr -> is_item)
1613 push_item(p, (oberon_item_t *) expr);
1615 else
1617 push_operator(p, (oberon_oper_t *) expr);
1621 static void
1622 store_expr(gen_proc_t * p, oberon_expr_t * dst, oberon_expr_t * src)
1624 assert(dst -> is_item);
1625 oberon_item_t * item = (oberon_item_t *) dst;
1627 if(dst -> result -> class == OBERON_TYPE_ARRAY
1628 || src -> result -> class == OBERON_TYPE_ARRAY)
1630 push_expr(p, dst);
1631 push_expr(p, src);
1632 jvm_generate_copy_array(p, dst -> result);
1634 else if(dst -> result -> class == OBERON_TYPE_RECORD
1635 || src -> result -> class == OBERON_TYPE_RECORD)
1637 push_expr(p, dst);
1638 push_expr(p, src);
1639 jvm_generate_copy_record(p, dst -> result);
1641 else switch(item -> mode)
1643 case MODE_VAR:
1644 push_expr(p, src);
1645 jvm_generate_store(p, src -> result, item -> var -> gen_var);
1646 break;
1647 case MODE_INDEX:
1649 char postfix = jvm_get_postfix(src -> result);
1650 int cell_size = jvm_cell_size_for_postfix(postfix);
1651 assert(item -> parent -> is_item);
1652 push_item(p, (oberon_item_t *) item -> parent);
1653 push_expr(p, item -> args);
1654 push_expr(p, src);
1655 jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", postfix);
1656 break;
1657 case MODE_FIELD:
1658 assert(item -> parent -> is_item);
1659 push_item(p, (oberon_item_t *) item -> parent);
1660 push_expr(p, src);
1661 jvm_generate_store(p, src -> result, item -> var -> gen_var);
1662 break;
1663 default:
1664 gen_error("store_expr: unk mode %i", item -> mode);
1665 break;
1669 void
1670 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
1672 gen_module_t * m;
1673 gen_proc_t * p;
1675 m = ctx -> mod -> gen_mod;
1676 p = m -> class -> p;
1678 store_expr(p, dst, src);
1681 void
1682 oberon_generate_code(oberon_context_t * ctx)
1684 printf("generate code\n");
1687 void
1688 oberon_generator_dump(oberon_context_t * ctx, char * path)
1690 printf("jit: dump code\n");
1693 void *
1694 oberon_generator_get_procedure(oberon_context_t * ctx, const char * name)
1696 printf("jit: get pointer to procedure %s\n", name);
1697 return NULL;
1700 void *
1701 oberon_generator_get_var(oberon_context_t * ctx, const char * name)
1703 printf("jit: get pointer to var %s\n", name);
1704 return NULL;