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);
830 fprintf(class -> fp, ".super java/lang/Object\n\n");
832 rec -> gen_type -> class = class;
835 void
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:
849 break;
850 case OBERON_TYPE_RECORD:
852 gen_module_t * m;
853 m = type -> module -> gen_mod;
854 oberon_generate_record_class(m, type);
855 break;
856 case OBERON_TYPE_PROCEDURE:
857 oberon_generate_procedure_class(type);
858 break;
859 case OBERON_TYPE_POINTER:
860 break;
861 default:
862 gen_error("oberon_generator_init_type: unk calss %i", type -> class);
863 break;
867 void
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;
892 field = rec -> 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;
918 field = rec -> 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);
937 else
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);
952 void
953 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
955 gen_var_t * v = oberon_generator_new_var();
956 var -> gen_var = v;
958 gen_module_t * m;
959 m = ctx -> mod -> gen_mod;
961 struct gen_class * class;
962 class = m -> class;
964 char * desc;
965 switch(var -> class)
967 case OBERON_CLASS_VAR_PARAM:
968 gen_error("generator: VAR-parameters not implemented");
969 break;
970 case OBERON_CLASS_PARAM:
971 /* Заполняется при генерации функции */
972 /* смотри jvm_init_local_object() */
973 break;
974 case OBERON_CLASS_FIELD:
975 /* Заполняются при инициализации структуры */
976 /* смотри oberon_generator_init_record() */
977 break;
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);
986 break;
987 default:
988 gen_error("oberon_generator_init_var: unk var class %i", var -> class);
989 break;
993 void
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);
999 if(proc -> local)
1001 gen_error("generator: local procedures not implemented");
1004 oberon_generate_procedure_pointer_class(proc);
1007 void
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);
1012 mod -> gen_mod = 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");
1021 m -> class = class;
1023 ctx -> gen_context -> current_m = m;
1026 void
1027 oberon_generator_fini_module(oberon_module_t * mod)
1029 jvm_destroy_class(mod -> gen_mod -> class);
1032 static void
1033 push_expr(gen_proc_t * p, oberon_expr_t * expr);
1035 void
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;
1048 while(x != NULL)
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);
1066 x = x -> next;
1070 void
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);
1080 static void
1081 jvm_init_local_object(gen_proc_t * p, oberon_object_t * x)
1083 gen_var_t * v;
1084 bool wide;
1085 char * desc;
1086 struct gen_class * class;
1088 v = x -> gen_var;
1089 class = p -> class;
1090 wide = jvm_is_wide_type(x -> type);
1091 desc = jvm_get_descriptor(x -> type);
1093 // Убеждаемся что сейчас находимся в функции
1094 assert(class -> p);
1095 assert(x -> local);
1097 switch(x -> class)
1099 case OBERON_CLASS_VAR:
1100 case OBERON_CLASS_PARAM:
1101 jvm_generate_and_init_named_local_var(v, p, wide, x -> name, desc);
1102 break;
1103 default:
1104 gen_error("jvm_init_local_object: wat");
1105 break;
1109 /*
1110 * Генерирует код для получения размера измерения массива
1111 * Аналог Обероновского LEN(v, n); где n = 0 - первое измерение.
1112 * ( aref -- int )
1113 */
1115 static void
1116 jvm_generate_array_len(gen_proc_t * p, int dim)
1118 while(dim > 0)
1120 jvm_generate(p, 0, 1, "iconst_0");
1121 jvm_generate(p, 2, 1, "aaload");
1122 dim -= 1;
1124 jvm_generate(p, 1, 1, "arraylength");
1127 static void
1128 jvm_generate_array_duplicate_and_replace(gen_proc_t * p, gen_var_t * v, oberon_type_t * arr)
1130 int dim = 0;
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);
1138 dim += 1;
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);
1150 static void
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);
1160 void
1161 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
1163 gen_proc_t * p;
1164 char * signature;
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;
1173 while(var)
1175 jvm_init_local_object(p, var);
1176 var = var -> next;
1179 /* Копирование статических/открытых массивов и записей */
1180 var = proc -> type -> decl;
1181 while(var)
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)
1190 {
1191 jvm_generate_record_duplicate_and_replace(p, var -> gen_var, var -> type);
1194 var = var -> next;
1195 }
1198 void
1199 oberon_generate_end_proc(oberon_context_t * ctx)
1201 gen_module_t * m;
1202 gen_proc_t * p;
1204 m = ctx -> mod -> gen_mod;
1205 p = m -> class -> p;
1207 jvm_generate_function_end(p);
1210 static void
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;
1217 char * fullname;
1218 char * signature;
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);
1230 int args_cells = 0;
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);
1238 push_expr(p, arg);
1239 arg = arg -> next;
1242 if(proc -> class == OBERON_CLASS_PROC)
1244 /* Обычная статическая процедура */
1245 jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", fullname, signature);
1247 else
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);
1255 void
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);
1261 void
1262 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
1264 gen_module_t * m;
1265 gen_proc_t * p;
1266 char prefix;
1267 int cell_size;
1269 m = ctx -> mod -> gen_mod;
1270 p = m -> class -> p;
1272 if(expr)
1274 push_expr(p, expr);
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);
1279 else
1281 jvm_generate(p, 0, 0, "return");
1285 static void
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++)
1292 push_expr(p, arg);
1293 arg = arg -> next;
1296 jvm_generate_new(p, type -> base, num);
1299 static void
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)
1311 return;
1314 jvm_generate(p, from_cell_size, to_cell_size, "%c2%c", prefix, postfix);
1317 static void
1318 push_item(gen_proc_t * p, oberon_item_t * item)
1320 switch(item -> mode)
1322 case MODE_VAR:
1323 if(item -> var -> class == OBERON_CLASS_PROC)
1325 jvm_generate_push_procedure_pointer(p, item -> var);
1327 else
1329 jvm_generate_load(p, item -> result, item -> var -> gen_var);
1331 break;
1332 case MODE_INTEGER:
1333 jvm_generate_push_int_size(p, item -> integer, item -> result -> size);
1334 break;
1335 case MODE_BOOLEAN:
1336 jvm_generate_push_int_size(p, item -> boolean, item -> result -> size);
1337 break;
1338 case MODE_CALL:
1339 jvm_generate_call_proc(p, (oberon_expr_t *) item);
1340 break;
1341 case MODE_INDEX:
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);
1349 break;
1350 case MODE_FIELD:
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);
1354 break;
1355 case MODE_DEREF:
1356 /* Все объекты в jvm представляются как указатели */
1357 assert(item -> parent -> is_item);
1358 push_item(p, (oberon_item_t *) item -> parent);
1359 break;
1360 case MODE_NIL:
1361 jvm_generate(p, 0, 1, "aconst_null");
1362 break;
1363 case MODE_NEW:
1364 jvm_generate_expr_new_pointer(p, item -> result, item -> num_args, item -> args);
1365 break;
1366 case MODE_REAL:
1367 jvm_generate_push_float(p, item -> real, item -> result -> size);
1368 break;
1369 case MODE_CAST:
1370 push_expr(p, item -> parent);
1371 jvm_generate_cast_type(p, item -> parent -> result, item -> result);
1372 break;
1373 default:
1374 gen_error("push_item: unk mode %i", item -> mode);
1375 break;
1379 static void
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);
1395 static void
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);
1403 static void
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 = "";
1415 switch(op)
1417 case OP_EQ:
1418 cmpop = "eq";
1419 break;
1420 case OP_NEQ:
1421 cmpop = "ne";
1422 break;
1423 case OP_LSS:
1424 cmpop = "lt";
1425 break;
1426 case OP_LEQ:
1427 cmpop = "le";
1428 break;
1429 case OP_GRT:
1430 cmpop = "gt";
1431 break;
1432 case OP_GEQ:
1433 cmpop = "ge";
1434 break;
1435 default:
1436 gen_error("jvm_generate_compare_op: wat");
1437 break;
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);
1448 static void
1449 jvm_generate_operator(gen_proc_t * p, char prefix, int op)
1451 int cell_size = jvm_cell_size_for_postfix(prefix);
1452 switch(op)
1454 case OP_UNARY_MINUS:
1455 jvm_generate(p, cell_size, cell_size, "%cneg", prefix);
1456 break;
1457 case OP_BITWISE_NOT:
1458 jvm_generate_push_int(p, -1);
1459 jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
1460 break;
1461 case OP_LOGIC_NOT:
1462 jvm_generate_logical_not(p);
1463 break;
1464 case OP_ABS:
1465 jvm_generate_abs(p, prefix);
1466 break;
1468 case OP_ADD:
1469 jvm_generate(p, 2 * cell_size, cell_size, "%cadd", prefix);
1470 break;
1471 case OP_SUB:
1472 jvm_generate(p, 2 * cell_size, cell_size, "%csub", prefix);
1473 break;
1474 case OP_MUL:
1475 jvm_generate(p, 2 * cell_size, cell_size, "%cmul", prefix);
1476 break;
1477 case OP_DIV:
1478 jvm_generate(p, 2 * cell_size, cell_size, "%cdiv", prefix);
1479 break;
1480 case OP_MOD:
1481 jvm_generate(p, 2 * cell_size, cell_size, "%crem", prefix);
1482 break;
1483 case OP_BITWISE_AND:
1484 jvm_generate(p, 2 * cell_size, cell_size, "%cand", prefix);
1485 break;
1486 case OP_BITWISE_XOR:
1487 jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
1488 break;
1489 case OP_BITWISE_OR:
1490 jvm_generate(p, 2 * cell_size, cell_size, "%cor", prefix);
1491 break;
1493 case OP_EQ:
1494 case OP_NEQ:
1495 case OP_LSS:
1496 case OP_LEQ:
1497 case OP_GRT:
1498 case OP_GEQ:
1499 jvm_generate_compare_op(p, prefix, op);
1500 break;
1501 default:
1502 gen_error("jvm_generate_operator: unk op %i", op);
1503 break;
1504 }
1507 static void
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 */
1517 push_expr(p, a);
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);
1522 push_expr(p, b);
1523 jvm_generate_label(p, label_done);
1526 static void
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 */
1536 push_expr(p, a);
1537 jvm_generate(p, 1, 0, "ifne %s", label_name_false);
1538 push_expr(p, b);
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);
1545 static void
1546 push_operator(gen_proc_t * p, oberon_oper_t * oper)
1548 char prefix = jvm_get_prefix(oper -> result);
1549 int op = oper -> op;
1550 switch(op)
1552 case OP_UNARY_MINUS:
1553 case OP_BITWISE_NOT:
1554 case OP_LOGIC_NOT:
1555 case OP_ABS:
1556 push_expr(p, oper -> left);
1557 jvm_generate_operator(p, prefix, op);
1558 break;
1560 case OP_ADD:
1561 case OP_SUB:
1562 case OP_MUL:
1563 case OP_DIV:
1564 case OP_MOD:
1565 case OP_BITWISE_AND:
1566 case OP_BITWISE_XOR:
1567 case OP_BITWISE_OR:
1569 case OP_EQ:
1570 case OP_NEQ:
1571 case OP_LSS:
1572 case OP_LEQ:
1573 case OP_GRT:
1574 case OP_GEQ:
1575 push_expr(p, oper -> left);
1576 push_expr(p, oper -> right);
1577 jvm_generate_operator(p, prefix, op);
1578 break;
1580 case OP_LOGIC_OR:
1581 jvm_generate_logical_or(p, oper -> left, oper -> right);
1582 break;
1583 case OP_LOGIC_AND:
1584 jvm_generate_logical_and(p, oper -> left, oper -> right);
1585 break;
1586 default:
1587 gen_error("push_oper: unk op %i", op);
1588 break;
1592 static void
1593 push_expr(gen_proc_t * p, oberon_expr_t * expr)
1595 if(expr -> is_item)
1597 push_item(p, (oberon_item_t *) expr);
1599 else
1601 push_operator(p, (oberon_oper_t *) expr);
1605 static void
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)
1614 push_expr(p, dst);
1615 push_expr(p, src);
1616 jvm_generate_copy_array(p, dst -> result);
1618 else if(dst -> result -> class == OBERON_TYPE_RECORD
1619 || src -> result -> class == OBERON_TYPE_RECORD)
1621 push_expr(p, dst);
1622 push_expr(p, src);
1623 jvm_generate_copy_record(p, dst -> result);
1625 else switch(item -> mode)
1627 case MODE_VAR:
1628 push_expr(p, src);
1629 jvm_generate_store(p, src -> result, item -> var -> gen_var);
1630 break;
1631 case MODE_INDEX:
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);
1638 push_expr(p, src);
1639 jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", postfix);
1640 break;
1641 case MODE_FIELD:
1642 assert(item -> parent -> is_item);
1643 push_item(p, (oberon_item_t *) item -> parent);
1644 push_expr(p, src);
1645 jvm_generate_store(p, src -> result, item -> var -> gen_var);
1646 break;
1647 default:
1648 gen_error("store_expr: unk mode %i", item -> mode);
1649 break;
1653 void
1654 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
1656 gen_module_t * m;
1657 gen_proc_t * p;
1659 m = ctx -> mod -> gen_mod;
1660 p = m -> class -> p;
1662 store_expr(p, dst, src);
1665 void
1666 oberon_generate_code(oberon_context_t * ctx)
1668 printf("generate code\n");
1671 void
1672 oberon_generator_dump(oberon_context_t * ctx, char * path)
1674 printf("jit: dump code\n");
1677 void *
1678 oberon_generator_get_procedure(oberon_context_t * ctx, const char * name)
1680 printf("jit: get pointer to procedure %s\n", name);
1681 return NULL;
1684 void *
1685 oberon_generator_get_var(oberon_context_t * ctx, const char * name)
1687 printf("jit: get pointer to var %s\n", name);
1688 return NULL;