DEADSOFTWARE

JVM: Добавлена инициализация статических полей записи при и использовании NEW
[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 }
53 void
54 jvm_generate_function_end(gen_proc_t * p)
55 {
56 struct gen_class * class;
57 class = p -> class;
59 assert(class -> p);
60 class -> p = NULL;
62 fprintf(class -> fp, " .limit stack %i\n", p -> stack -> max_pointer);
63 fprintf(class -> fp, " .limit locals %i\n", p -> rf -> max_used);
64 fprintf(class -> fp, ".end method\n\n");
65 }
67 struct gen_class *
68 jvm_create_class(char * full_name)
69 {
70 struct gen_class * class = GC_MALLOC(sizeof *class);
71 memset(class, 0, sizeof *class);
73 char * fname = new_string("%s.j", full_name);
74 class -> full_name = new_string(full_name);
75 class -> fp = fopen(fname, "w");
76 assert(class -> fp);
78 return class;
79 }
81 static void
82 jvm_destroy_class(struct gen_class * class)
83 {
84 assert(class -> p == NULL);
85 fclose(class -> fp);
86 }
88 static void
89 jvm_stack_push(gen_proc_t * p, unsigned size)
90 {
91 p -> stack -> pointer += size;
92 if(p -> stack -> pointer > p -> stack -> max_pointer)
93 {
94 p -> stack -> max_pointer = p -> stack -> pointer;
95 }
96 }
98 static void
99 jvm_stack_pop(gen_proc_t * p, unsigned size)
101 p -> stack -> pointer -= size;
102 assert(p -> stack -> pointer >= 0);
105 static void
106 jvm_generate(gen_proc_t * p, unsigned get, unsigned push, char * format, ...)
108 va_list ptr;
109 va_start(ptr, format);
111 jvm_stack_pop(p, get);
112 fprintf(p -> class -> fp, " ");
113 vfprintf(p -> class -> fp, format, ptr);
114 fprintf(p -> class -> fp, "\n");
115 jvm_stack_push(p, push);
117 va_end(ptr);
120 static void
121 jvm_generate_push_int(gen_proc_t * p, int64_t i)
123 if(i == -1)
125 jvm_generate(p, 0, 1, "iconst_m1");
127 else if(i >= 0 && i <= 5)
129 jvm_generate(p, 0, 1, "iconst_%li", i);
131 else if(i >= -128 && i <= 127)
133 jvm_generate(p, 0, 1, "bipush %li", i);
135 else if(i >= -32768 && i <= 32767)
137 jvm_generate(p, 0, 1, "sipush %li", i);
139 else if(i >= -2147483648 && i <= 2147483647)
141 jvm_generate(p, 0, 1, "ldc %li", i);
143 else
145 jvm_generate(p, 0, 2, "ldc2 %li", i);
149 static void
150 jvm_generate_push_float(gen_proc_t * p, double f, int size)
152 if(size <= 4)
154 if(f == 0.0)
156 jvm_generate(p, 0, 1, "fconst_0");
158 if(f == 1.0)
160 jvm_generate(p, 0, 1, "fconst_1");
162 if(f == 2.0)
164 jvm_generate(p, 0, 1, "fconst_2");
166 else
168 jvm_generate(p, 0, 1, "ldc %lf", f);
171 else
173 if(f == 0.0)
175 jvm_generate(p, 0, 2, "dconst_0");
177 if(f == 1.0)
179 jvm_generate(p, 0, 2, "dconst_1");
181 else
183 jvm_generate(p, 0, 2, "ldc2 %lf", f);
188 static int
189 jvm_new_label_id(gen_proc_t * p)
191 int label_id = p -> label_id;
192 p -> label_id += 1;
193 return label_id;
196 static char *
197 jvm_get_label_name(int label_id)
199 return new_string("L%i", label_id);
202 static void
203 jvm_generate_label(gen_proc_t * p, int label_id)
205 jvm_generate(p, 0, 0, "L%i:", label_id);
208 static void
209 jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type)
211 char * cname;
213 switch(type -> class)
215 case OBERON_TYPE_INTEGER:
216 case OBERON_TYPE_BOOLEAN:
217 case OBERON_TYPE_PROCEDURE:
218 case OBERON_TYPE_REAL:
219 case OBERON_TYPE_POINTER:
220 /* ничего не надо делать при статической инициализации */
221 break;
222 case OBERON_TYPE_RECORD:
223 cname = jvm_get_class_full_name(type);
224 jvm_generate(p, 0, 1, "new %s", cname);
225 jvm_generate(p, 1, 2, "dup");
226 jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", cname);
227 break;
228 case OBERON_TYPE_ARRAY:
229 gen_error("jvm_generate_new_static: TODO array");
230 break;
231 default:
232 gen_error("jvm_generate_new_static: unk type class %i", type -> class);
233 break;
237 static void
238 jvm_generate_store(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * dst)
240 char prefix;
241 int cell_size;
243 cell_size = jvm_cell_size_for_type(src_type);
245 switch(dst -> storage)
247 case JVM_STORAGE_REGISTER:
248 prefix = jvm_get_prefix(src_type);
249 jvm_generate(p, cell_size, 0, "%cstore %i", prefix, dst -> reg);
250 break;
251 case JVM_STORAGE_STATIC:
252 jvm_generate(p, cell_size, 0, "putstatic %s %s", dst -> full_name, dst -> desc);
253 break;
254 case JVM_STORAGE_FIELD:
255 jvm_generate(p, 1 + cell_size, 0, "putfield %s %s", dst -> full_name, dst -> desc);
256 break;
257 default:
258 gen_error("jvm_generate_store: unknow storage type %i", dst -> storage);
259 break;
263 static gen_var_t *
264 oberon_generator_new_var()
266 gen_var_t * v = GC_MALLOC(sizeof *v);
267 memset(v, 0, sizeof *v);
268 return v;
271 static void
272 jvm_generate_and_init_global_var(gen_var_t * v, struct gen_class * class, char * access, char * name, char * desc)
274 assert(class -> p == NULL);
275 fprintf(class -> fp, ".field %s static %s %s\n\n", access, name, desc);
277 v -> storage = JVM_STORAGE_STATIC;
278 v -> full_name = new_string("%s/%s", class -> full_name, name);
279 v -> desc = new_string(desc);
282 static void
283 jvm_generate_and_init_field(gen_var_t * v, struct gen_class * class, char * access, char * name, char * desc)
285 assert(class -> p == NULL);
286 fprintf(class -> fp, ".field %s %s %s\n\n", access, name, desc);
288 v -> storage = JVM_STORAGE_FIELD;
289 v -> full_name = new_string("%s/%s", class -> full_name, name);
290 v -> desc = new_string(desc);
293 static void
294 jvm_generate_and_init_local_var(gen_var_t * v, gen_proc_t * p, bool wide)
296 v -> storage = JVM_STORAGE_REGISTER;
297 v -> reg = jvm_alloc_register_untyped(p -> rf, wide);
300 static void
301 jvm_generate_and_init_named_local_var(gen_var_t * v, gen_proc_t * p, bool wide, char * name, char * desc)
303 jvm_generate_and_init_local_var(v, p, wide);
304 v -> desc = new_string(desc);
305 jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", v -> reg, name, desc);
308 // ==========================================
309 // ==========================================
310 // ==========================================
312 void
313 oberon_generator_init_context(oberon_context_t * ctx)
315 gen_context_t * gen_context = GC_MALLOC(sizeof *gen_context);
316 memset(gen_context, 0, sizeof *gen_context);
318 ctx -> gen_context = gen_context;
321 void
322 oberon_generator_destroy_context(oberon_context_t * ctx)
326 static void
327 oberon_generate_procedure_class(oberon_type_t * proc)
329 FILE * fp;
330 char * cname;
331 char * signature;
332 struct gen_class * class;
334 cname = jvm_get_class_full_name(proc);
335 class = jvm_create_class(cname);
336 fp = class -> fp;
338 fprintf(fp, ".source SYSTEM\n");
339 fprintf(fp, ".class public abstract %s\n", cname);
340 fprintf(fp, ".super java/lang/Object\n\n");
342 fprintf(fp, ".method public <init>()V\n");
343 fprintf(fp, " aload_0\n");
344 fprintf(fp, " invokespecial java/lang/Object/<init>()V\n");
345 fprintf(fp, " return\n");
346 fprintf(fp, ".end method\n\n");
348 signature = jvm_get_procedure_signature(proc);
350 fprintf(fp, ".method public abstract invoke%s\n", signature);
351 fprintf(fp, ".end method\n\n");
353 jvm_destroy_class(class);
356 static void
357 oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec)
359 char * cname;
360 struct gen_class * class;
362 /* Устанавливаем новоый id */
363 rec -> gen_type -> rec_id = m -> rec_id;
364 m -> rec_id += 1;
366 cname = jvm_get_class_full_name(rec);
367 class = jvm_create_class(cname);
369 fprintf(class -> fp, ".source %s\n", rec -> module -> name);
370 fprintf(class -> fp, ".class public %s\n", cname);
371 fprintf(class -> fp, ".super java/lang/Object\n\n");
373 rec -> gen_type -> class = class;
376 void
377 oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type)
379 gen_type_t * t = GC_MALLOC(sizeof *t);
380 memset(t, 0, sizeof *t);
381 type -> gen_type = t;
383 switch(type -> class)
385 case OBERON_TYPE_VOID:
386 case OBERON_TYPE_INTEGER:
387 case OBERON_TYPE_BOOLEAN:
388 case OBERON_TYPE_ARRAY:
389 case OBERON_TYPE_REAL:
390 break;
391 case OBERON_TYPE_RECORD:
393 gen_module_t * m;
394 m = type -> module -> gen_mod;
395 oberon_generate_record_class(m, type);
396 break;
397 case OBERON_TYPE_PROCEDURE:
398 oberon_generate_procedure_class(type);
399 break;
400 case OBERON_TYPE_POINTER:
401 break;
402 default:
403 gen_error("oberon_generator_init_type: unk calss %i", type -> class);
404 break;
408 void
409 oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
411 struct gen_class * class;
412 class = rec -> gen_type -> class;
414 int num = rec -> num_decl;
415 oberon_object_t * field = rec -> decl;
416 for(int i = 0; i < num; i++)
418 gen_var_t * v = field -> gen_var;
419 char * name = field -> name;
420 char * desc = jvm_get_descriptor(field -> type);
421 jvm_generate_and_init_field(v, class, "public", name, desc);
422 field = field -> next;
425 gen_proc_t * p = jvm_create_proc(class);
427 jvm_generate_function_header(p, "public", "<init>", "()V");
428 jvm_alloc_register_untyped(p -> rf, false);
429 jvm_generate(p, 0, 1, "aload_0");
430 jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
432 num = rec -> num_decl;
433 field = rec -> decl;
434 for(int i = 0; i < num; i++)
436 if(field -> type -> class == OBERON_TYPE_RECORD
437 || field -> type -> class == OBERON_TYPE_ARRAY)
439 jvm_generate(p, 0, 1, "aload_0");
440 jvm_generate_new_static(p, field -> type);
441 jvm_generate_store(p, field -> type, field -> gen_var);
443 field = field -> next;
444 }
446 jvm_generate(p, 0, 0, "return");
447 jvm_generate_function_end(p);
449 jvm_destroy_class(class);
452 void
453 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
455 gen_var_t * v = oberon_generator_new_var();
456 var -> gen_var = v;
458 gen_module_t * m;
459 m = ctx -> mod -> gen_mod;
461 struct gen_class * class;
462 class = m -> class;
464 char * desc;
465 switch(var -> class)
467 case OBERON_CLASS_VAR_PARAM:
468 gen_error("generator: VAR-parameters not implemented");
469 break;
470 case OBERON_CLASS_PARAM:
471 /* Заполняется при генерации функции */
472 /* смотри jvm_init_local_object() */
473 break;
474 case OBERON_CLASS_FIELD:
475 /* Заполняются при инициализации структуры */
476 /* смотри oberon_generator_init_record() */
477 break;
478 case OBERON_CLASS_VAR:
479 /* Локальные заполняются при генерации функции */
480 /* смотри jvm_init_local_object() */
481 if(var -> local == 0)
483 desc = jvm_get_descriptor(var -> type);
484 jvm_generate_and_init_global_var(v, class, "public", var -> name, desc);
486 break;
487 default:
488 gen_error("oberon_generator_init_var: unk var class %i", var -> class);
489 break;
493 void
494 oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
496 struct gen_class * class = proc -> module -> gen_mod -> class;
497 proc -> gen_proc = jvm_create_proc(class);
499 if(proc -> local)
501 gen_error("generator: local procedures not implemented");
505 void
506 oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod)
508 gen_module_t * m = GC_MALLOC(sizeof *m);
509 memset(m, 0, sizeof *m);
510 mod -> gen_mod = m;
512 struct gen_class * class;
513 class = jvm_create_class(mod -> name);
515 fprintf(class -> fp, ".source %s\n", mod -> name);
516 fprintf(class -> fp, ".class %s\n", mod -> name);
517 fprintf(class -> fp, ".super java/lang/Object\n\n");
519 m -> class = class;
521 ctx -> gen_context -> current_m = m;
524 void
525 oberon_generator_fini_module(oberon_module_t * mod)
527 jvm_destroy_class(mod -> gen_mod -> class);
530 static void
531 push_expr(gen_proc_t * p, oberon_expr_t * expr);
533 void
534 oberon_generate_begin_module(oberon_context_t * ctx)
536 struct gen_class * class = ctx -> mod -> gen_mod -> class;
537 gen_proc_t * p = jvm_create_proc(class);
539 jvm_generate_function_header(p, "public", "<init>", "()V");
540 jvm_alloc_register_untyped(p -> rf, false);
541 jvm_generate(p, 0, 1, "aload_0");
542 jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
545 void
546 oberon_generate_end_module(oberon_context_t * ctx)
548 struct gen_class * class = ctx -> mod -> gen_mod -> class;
549 gen_proc_t * p = class -> p;
551 jvm_generate(p, 0, 0, "return");
552 jvm_generate_function_end(class -> p);
555 static void
556 jvm_init_local_object(gen_proc_t * p, oberon_object_t * x)
558 gen_var_t * v;
559 bool wide;
560 char * desc;
561 struct gen_class * class;
563 v = x -> gen_var;
564 class = p -> class;
565 wide = jvm_is_wide_type(x -> type);
566 desc = jvm_get_descriptor(x -> type);
568 // Убеждаемся что сейчас находимся в функции
569 assert(class -> p);
570 assert(x -> local);
572 switch(x -> class)
574 case OBERON_CLASS_VAR:
575 case OBERON_CLASS_PARAM:
576 jvm_generate_and_init_named_local_var(v, p, wide, x -> name, desc);
577 break;
578 default:
579 gen_error("jvm_init_local_object: wat");
580 break;
584 void
585 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
587 gen_proc_t * p;
588 char * signature;
590 p = proc -> gen_proc;
592 signature = jvm_get_procedure_signature(proc -> type);
594 jvm_generate_function_header(p, "public static", proc -> name, signature);
596 oberon_object_t * var = proc -> type -> decl;
597 while(var)
599 jvm_init_local_object(p, var);
600 var = var -> next;
604 void
605 oberon_generate_end_proc(oberon_context_t * ctx)
607 gen_module_t * m;
608 gen_proc_t * p;
610 m = ctx -> mod -> gen_mod;
611 p = m -> class -> p;
613 jvm_generate_function_end(p);
616 static void
617 jvm_generate_call_proc(gen_proc_t * p, oberon_expr_t * desig)
619 assert(desig -> is_item);
620 assert(desig -> item.mode == MODE_CALL);
622 oberon_object_t * proc;
623 char * fullname;
624 char * signature;
626 proc = desig -> item.var;
627 fullname = jvm_get_field_full_name(proc);
628 signature = jvm_get_procedure_signature(proc -> type);
630 int args_cells = 0;
631 int result_cells = jvm_cell_size_for_type(proc -> type -> base);
633 int num = desig -> item.num_args;
634 oberon_expr_t * arg = desig -> item.args;
635 for(int i = 0; i < num; i++)
637 args_cells += jvm_cell_size_for_type(arg -> result);
638 push_expr(p, arg);
639 arg = arg -> next;
642 jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", fullname, signature);
645 void
646 oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
648 jvm_generate_call_proc(ctx -> gen_context -> current_m -> class -> p, desig);
651 void
652 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
654 gen_module_t * m;
655 gen_proc_t * p;
656 char prefix;
657 int cell_size;
659 m = ctx -> mod -> gen_mod;
660 p = m -> class -> p;
662 if(expr)
664 push_expr(p, expr);
665 prefix = jvm_get_prefix(expr -> result);
666 cell_size = jvm_cell_size_for_type(expr -> result);
667 jvm_generate(p, cell_size, 0, "%creturn", prefix);
669 else
671 jvm_generate(p, 0, 0, "return");
675 static void
676 jvm_push_var(gen_proc_t * p, oberon_object_t * var)
678 int cell_size = jvm_cell_size_for_type(var -> type);
680 if(var -> local)
682 int reg = var -> gen_var -> reg;
683 char prefix = jvm_get_prefix(var -> type);
684 jvm_generate(p, 0, cell_size, "%cload %i", prefix, reg);
686 else
688 char * fullname = jvm_get_field_full_name(var);
689 char * desc = jvm_get_descriptor(var -> type);
690 jvm_generate(p, 0, cell_size, "getstatic %s %s", fullname, desc);
694 static void
695 jvm_generate_expr_new_static(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
697 assert(num == 0);
698 jvm_generate_new_static(p, type);
701 static void
702 jvm_generate_expr_new_pointer(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
704 assert(type -> class == OBERON_TYPE_POINTER);
705 jvm_generate_expr_new_static(p, type -> base, num, arg);
708 static void
709 push_item(gen_proc_t * p, oberon_item_t * item)
711 switch(item -> mode)
713 case MODE_VAR:
714 jvm_push_var(p, item -> var);
715 break;
716 case MODE_INTEGER:
717 jvm_generate_push_int(p, item -> integer);
718 break;
719 case MODE_BOOLEAN:
720 jvm_generate_push_int(p, item -> boolean);
721 break;
722 case MODE_CALL:
723 jvm_generate_call_proc(p, (oberon_expr_t *) item);
724 break;
725 case MODE_INDEX:
726 gen_error("push_item: TODO index");
727 break;
728 case MODE_FIELD:
729 push_item(p, item -> parent);
730 char * field = jvm_get_field_full_name(item -> var);
731 char * desc = jvm_get_descriptor(item -> var -> type);
732 jvm_generate(p, 1, 1, "getfield %s %s", field, desc);
733 break;
734 case MODE_DEREF:
735 /* Все объекты представляются как увказатели */
736 push_item(p, item -> parent);
737 break;
738 case MODE_NIL:
739 jvm_generate(p, 0, 1, "aconst_null");
740 break;
741 case MODE_NEW:
742 jvm_generate_expr_new_pointer(p, item -> result, item -> num_args, item -> args);
743 break;
744 case MODE_REAL:
745 jvm_generate_push_float(p, item -> real, item -> result -> size);
746 break;
747 default:
748 gen_error("push_item: unk mode %i", item -> mode);
749 break;
753 static void
754 jvm_generate_logical_not(gen_proc_t * p)
756 int label_done = jvm_new_label_id(p);
757 int label_false = jvm_new_label_id(p);
758 char * label_name_done = jvm_get_label_name(label_done);
759 char * label_name_false = jvm_get_label_name(label_false);
761 jvm_generate(p, 1, 0, "ifne %s", label_name_false);
762 jvm_generate(p, 0, 1, "iconst_1");
763 jvm_generate(p, 0, 0, "goto %s", label_name_done);
764 jvm_generate_label(p, label_false);
765 jvm_generate(p, 0, 1, "iconst_0");
766 jvm_generate_label(p, label_done);
769 static void
770 jvm_generate_abs(gen_proc_t * p, char prefix)
772 char t = jvm_get_type_of_prefix(prefix);
773 int cell_size = jvm_cell_size_for_prefix(prefix);
774 jvm_generate(p, cell_size, cell_size, "invokestatic java/lang/Math/abs(%c)%c", t, t);
777 static void
778 jvm_generate_compare_op(gen_proc_t * p, char prefix, int op)
780 int label_true = jvm_new_label_id(p);
781 int label_done = jvm_new_label_id(p);
782 char * label_name_true = jvm_get_label_name(label_true);
783 char * label_name_done = jvm_get_label_name(label_done);
784 int cell_size = 2 * jvm_cell_size_for_prefix(prefix);
786 assert(prefix == 'i' || prefix == 'a');
788 const char * cmpop = "";
789 switch(op)
791 case OP_EQ:
792 cmpop = "eq";
793 break;
794 case OP_NEQ:
795 cmpop = "ne";
796 break;
797 case OP_LSS:
798 cmpop = "lt";
799 break;
800 case OP_LEQ:
801 cmpop = "le";
802 break;
803 case OP_GRT:
804 cmpop = "gt";
805 break;
806 case OP_GEQ:
807 cmpop = "ge";
808 break;
809 default:
810 gen_error("jvm_generate_compare_op: wat");
811 break;
814 jvm_generate(p, cell_size, 0, "if_%ccmp%s %s", prefix, cmpop, label_name_true);
815 jvm_generate(p, 0, 1, "iconst_0");
816 jvm_generate(p, 0, 0, "goto %s", label_name_done);
817 jvm_generate_label(p, label_true);
818 jvm_generate(p, 0, 1, "iconst_1");
819 jvm_generate_label(p, label_done);
822 static void
823 jvm_generate_operator(gen_proc_t * p, char prefix, int op)
825 int cell_size = jvm_cell_size_for_prefix(prefix);
826 switch(op)
828 case OP_UNARY_MINUS:
829 jvm_generate(p, cell_size, cell_size, "%cneg", prefix);
830 break;
831 case OP_BITWISE_NOT:
832 jvm_generate_push_int(p, -1);
833 jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
834 break;
835 case OP_LOGIC_NOT:
836 jvm_generate_logical_not(p);
837 break;
838 case OP_ABS:
839 jvm_generate_abs(p, prefix);
840 break;
842 case OP_ADD:
843 jvm_generate(p, 2 * cell_size, cell_size, "%cadd", prefix);
844 break;
845 case OP_SUB:
846 jvm_generate(p, 2 * cell_size, cell_size, "%csub", prefix);
847 break;
848 case OP_MUL:
849 jvm_generate(p, 2 * cell_size, cell_size, "%cmul", prefix);
850 break;
851 case OP_DIV:
852 jvm_generate(p, 2 * cell_size, cell_size, "%cdiv", prefix);
853 break;
854 case OP_MOD:
855 jvm_generate(p, 2 * cell_size, cell_size, "%crem", prefix);
856 break;
857 case OP_BITWISE_AND:
858 jvm_generate(p, 2 * cell_size, cell_size, "%cand", prefix);
859 break;
860 case OP_BITWISE_XOR:
861 jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix);
862 break;
863 case OP_BITWISE_OR:
864 jvm_generate(p, 2 * cell_size, cell_size, "%cor", prefix);
865 break;
867 case OP_EQ:
868 case OP_NEQ:
869 case OP_LSS:
870 case OP_LEQ:
871 case OP_GRT:
872 case OP_GEQ:
873 jvm_generate_compare_op(p, prefix, op);
874 break;
875 default:
876 gen_error("jvm_generate_operator: unk op %i", op);
877 break;
878 }
881 static void
882 jvm_generate_logical_or(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
884 int label_calc_b = jvm_new_label_id(p);
885 int label_done = jvm_new_label_id(p);
886 char * label_name_calc_b = jvm_get_label_name(label_calc_b);
887 char * label_name_done = jvm_get_label_name(label_done);
889 /* a OR b -- если a, то TRUE, иначе b */
891 push_expr(p, a);
892 jvm_generate(p, 1, 0, "ifne %s", label_name_calc_b);
893 jvm_generate(p, 0, 1, "iconst_1");
894 jvm_generate(p, 0, 0, "goto %s", label_name_done);
895 jvm_generate_label(p, label_calc_b);
896 push_expr(p, b);
897 jvm_generate_label(p, label_done);
900 static void
901 jvm_generate_logical_and(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
903 int label_false = jvm_new_label_id(p);
904 int label_done = jvm_new_label_id(p);
905 char * label_name_false = jvm_get_label_name(label_false);
906 char * label_name_done = jvm_get_label_name(label_done);
908 /* a AND b -- если a, то b, иначе FALSE */
910 push_expr(p, a);
911 jvm_generate(p, 1, 0, "ifne %s", label_name_false);
912 push_expr(p, b);
913 jvm_generate(p, 0, 0, "goto %s", label_name_done);
914 jvm_generate_label(p, label_false);
915 jvm_generate(p, 0, 1, "iconst_0");
916 jvm_generate_label(p, label_done);
919 static void
920 push_operator(gen_proc_t * p, oberon_oper_t * oper)
922 char prefix = jvm_get_prefix(oper -> result);
923 int op = oper -> op;
924 switch(op)
926 case OP_UNARY_MINUS:
927 case OP_BITWISE_NOT:
928 case OP_LOGIC_NOT:
929 case OP_ABS:
930 push_expr(p, oper -> left);
931 jvm_generate_operator(p, prefix, op);
932 break;
934 case OP_ADD:
935 case OP_SUB:
936 case OP_MUL:
937 case OP_DIV:
938 case OP_MOD:
939 case OP_BITWISE_AND:
940 case OP_BITWISE_XOR:
941 case OP_BITWISE_OR:
943 case OP_EQ:
944 case OP_NEQ:
945 case OP_LSS:
946 case OP_LEQ:
947 case OP_GRT:
948 case OP_GEQ:
949 push_expr(p, oper -> left);
950 push_expr(p, oper -> right);
951 jvm_generate_operator(p, prefix, op);
952 break;
954 case OP_LOGIC_OR:
955 jvm_generate_logical_or(p, oper -> left, oper -> right);
956 break;
957 case OP_LOGIC_AND:
958 jvm_generate_logical_and(p, oper -> left, oper -> right);
959 break;
960 default:
961 gen_error("push_oper: unk op %i", op);
962 break;
966 static void
967 push_expr(gen_proc_t * p, oberon_expr_t * expr)
969 if(expr -> is_item)
971 push_item(p, (oberon_item_t *) expr);
973 else
975 push_operator(p, (oberon_oper_t *) expr);
979 static void
980 store_expr(gen_proc_t * p, oberon_expr_t * dst, oberon_expr_t * src)
982 assert(dst -> is_item);
983 oberon_item_t * item = (oberon_item_t *) dst;
985 switch(item -> mode)
987 case MODE_VAR:
988 push_expr(p, src);
989 jvm_generate_store(p, src -> result, item -> var -> gen_var);
990 break;
991 case MODE_INDEX:
992 gen_error("store_expr: TODO index");
993 break;
994 case MODE_FIELD:
995 push_item(p, item -> parent);
996 push_expr(p, src);
997 jvm_generate_store(p, src -> result, item -> var -> gen_var);
998 break;
999 default:
1000 gen_error("store_expr: unk mode %i", item -> mode);
1001 break;
1005 /*
1006 static void
1007 store_expr(gen_proc_t * p, oberon_expr_t * dst, oberon_expr_t * src)
1009 assert(dst -> is_item);
1010 oberon_item_t * item = (oberon_item_t *) dst;
1012 int cell_size = jvm_cell_size_for_type(src -> result);
1014 char prefix;
1015 switch(item -> mode)
1017 case MODE_VAR:
1018 push_expr(p, src);
1019 if(item -> var -> local)
1021 int reg = item -> var -> gen_var -> reg;
1022 prefix = jvm_get_prefix(item -> result);
1023 jvm_generate(p, cell_size, 0, "%cstore %i", prefix, reg);
1025 else
1027 char * fullname = jvm_get_field_full_name(item -> var);
1028 char * desc = jvm_get_descriptor(item -> result);
1029 jvm_generate(p, cell_size, 0, "putstatic %s %s", fullname, desc);
1031 break;
1032 case MODE_INDEX:
1033 gen_error("store_expr: TODO index");
1034 break;
1035 case MODE_FIELD:
1037 char * fullname = jvm_get_field_full_name(item -> var);
1038 char * desc = jvm_get_descriptor(item -> result);
1039 push_item(p, item -> parent);
1040 push_expr(p, src);
1041 jvm_generate(p, 1 + cell_size, 0, "putfield %s %s", fullname, desc);
1043 break;
1044 default:
1045 gen_error("store_expr: unk mode %i", item -> mode);
1046 break;
1049 */
1051 void
1052 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
1054 gen_module_t * m;
1055 gen_proc_t * p;
1057 m = ctx -> mod -> gen_mod;
1058 p = m -> class -> p;
1060 store_expr(p, dst, src);
1063 void
1064 oberon_generate_code(oberon_context_t * ctx)
1066 printf("generate code\n");
1069 void
1070 oberon_generator_dump(oberon_context_t * ctx, char * path)
1072 printf("jit: dump code\n");
1075 void *
1076 oberon_generator_get_procedure(oberon_context_t * ctx, const char * name)
1078 printf("jit: get pointer to procedure %s\n", name);
1079 return NULL;
1082 void *
1083 oberon_generator_get_var(oberon_context_t * ctx, const char * name)
1085 printf("jit: get pointer to var %s\n", name);
1086 return NULL;