11 #include "../../../include/oberon.h"
12 #include "../../oberon-internals.h"
13 #include "generator-jvm.h"
14 #include "generator-jvm-basic.h"
16 // ==========================================
17 // ==========================================
18 // ==========================================
21 jvm_create_proc(struct gen_class
* class)
23 gen_proc_t
* p
= GC_MALLOC(sizeof *p
);
24 memset(p
, 0, sizeof *p
);
26 struct gen_register_file
* rf
= GC_MALLOC(sizeof *rf
);
27 memset(rf
, 0, sizeof *rf
);
29 struct gen_stack
* stack
= GC_MALLOC(sizeof *stack
);
30 memset(rf
, 0, sizeof *stack
);
41 jvm_generate_function_header(gen_proc_t
* p
, char * access
, char * name
, char * signature
)
43 struct gen_class
* class;
46 /* Делаем процедуру текущей в этом классе */
47 assert(class -> p
== NULL
);
50 fprintf(class -> fp
, ".method %s %s%s\n", access
, name
, signature
);
54 jvm_generate_function_end(gen_proc_t
* p
)
56 struct gen_class
* class;
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");
68 jvm_create_class(char * full_name
)
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");
82 jvm_destroy_class(struct gen_class
* class)
84 assert(class -> p
== NULL
);
89 jvm_stack_push(gen_proc_t
* p
, unsigned size
)
91 p
-> stack
-> pointer
+= size
;
92 if(p
-> stack
-> pointer
> p
-> stack
-> max_pointer
)
94 p
-> stack
-> max_pointer
= p
-> stack
-> pointer
;
99 jvm_stack_pop(gen_proc_t
* p
, unsigned size
)
101 p
-> stack
-> pointer
-= size
;
102 assert(p
-> stack
-> pointer
>= 0);
106 jvm_generate(gen_proc_t
* p
, unsigned get
, unsigned push
, char * format
, ...)
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
);
121 jvm_generate_push_int(gen_proc_t
* p
, int64_t i
)
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
);
145 jvm_generate(p
, 0, 2, "ldc2 %li", i
);
150 jvm_generate_push_float(gen_proc_t
* p
, double f
, int size
)
156 jvm_generate(p
, 0, 1, "fconst_0");
160 jvm_generate(p
, 0, 1, "fconst_1");
164 jvm_generate(p
, 0, 1, "fconst_2");
168 jvm_generate(p
, 0, 1, "ldc %lf", f
);
175 jvm_generate(p
, 0, 2, "dconst_0");
179 jvm_generate(p
, 0, 2, "dconst_1");
183 jvm_generate(p
, 0, 2, "ldc2 %lf", f
);
189 jvm_new_label_id(gen_proc_t
* p
)
191 int label_id
= p
-> label_id
;
197 jvm_get_label_name(int label_id
)
199 return new_string("L%i", label_id
);
203 jvm_generate_label(gen_proc_t
* p
, int label_id
)
205 jvm_generate(p
, 0, 0, "L%i:", label_id
);
209 jvm_generate_new_static(gen_proc_t
* p
, oberon_type_t
* type
)
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 /* ничего не надо делать при статической инициализации */
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
);
228 case OBERON_TYPE_ARRAY
:
229 gen_error("jvm_generate_new_static: TODO array");
232 gen_error("jvm_generate_new_static: unk type class %i", type
-> class);
238 jvm_generate_store(gen_proc_t
* p
, oberon_type_t
* src_type
, gen_var_t
* dst
)
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
);
251 case JVM_STORAGE_STATIC
:
252 jvm_generate(p
, cell_size
, 0, "putstatic %s %s", dst
-> full_name
, dst
-> desc
);
254 case JVM_STORAGE_FIELD
:
255 jvm_generate(p
, 1 + cell_size
, 0, "putfield %s %s", dst
-> full_name
, dst
-> desc
);
258 gen_error("jvm_generate_store: unknow storage type %i", dst
-> storage
);
264 oberon_generator_new_var()
266 gen_var_t
* v
= GC_MALLOC(sizeof *v
);
267 memset(v
, 0, sizeof *v
);
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
);
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
);
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
);
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 // ==========================================
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
;
322 oberon_generator_destroy_context(oberon_context_t
* ctx
)
327 oberon_generate_procedure_class(oberon_type_t
* proc
)
332 struct gen_class
* class;
334 cname
= jvm_get_class_full_name(proc
);
335 class = jvm_create_class(cname
);
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);
357 oberon_generate_record_class(gen_module_t
* m
, oberon_type_t
* rec
)
360 struct gen_class
* class;
362 /* Устанавливаем новоый id */
363 rec
-> gen_type
-> rec_id
= m
-> rec_id
;
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;
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
:
391 case OBERON_TYPE_RECORD
:
394 m
= type
-> module
-> gen_mod
;
395 oberon_generate_record_class(m
, type
);
397 case OBERON_TYPE_PROCEDURE
:
398 oberon_generate_procedure_class(type
);
400 case OBERON_TYPE_POINTER
:
403 gen_error("oberon_generator_init_type: unk calss %i", type
-> class);
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
;
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
;
446 jvm_generate(p
, 0, 0, "return");
447 jvm_generate_function_end(p
);
449 jvm_destroy_class(class);
453 oberon_generator_init_var(oberon_context_t
* ctx
, oberon_object_t
* var
)
455 gen_var_t
* v
= oberon_generator_new_var();
459 m
= ctx
-> mod
-> gen_mod
;
461 struct gen_class
* class;
467 case OBERON_CLASS_VAR_PARAM
:
468 gen_error("generator: VAR-parameters not implemented");
470 case OBERON_CLASS_PARAM
:
471 /* Заполняется при генерации функции */
472 /* смотри jvm_init_local_object() */
474 case OBERON_CLASS_FIELD
:
475 /* Заполняются при инициализации структуры */
476 /* смотри oberon_generator_init_record() */
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
);
488 gen_error("oberon_generator_init_var: unk var class %i", var
-> class);
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);
501 gen_error("generator: local procedures not implemented");
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
);
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");
521 ctx
-> gen_context
-> current_m
= m
;
525 oberon_generator_fini_module(oberon_module_t
* mod
)
527 jvm_destroy_class(mod
-> gen_mod
-> class);
531 push_expr(gen_proc_t
* p
, oberon_expr_t
* expr
);
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");
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
);
556 jvm_init_local_object(gen_proc_t
* p
, oberon_object_t
* x
)
561 struct gen_class
* class;
565 wide
= jvm_is_wide_type(x
-> type
);
566 desc
= jvm_get_descriptor(x
-> type
);
568 // Убеждаемся что сейчас находимся в функции
574 case OBERON_CLASS_VAR
:
575 case OBERON_CLASS_PARAM
:
576 jvm_generate_and_init_named_local_var(v
, p
, wide
, x
-> name
, desc
);
579 gen_error("jvm_init_local_object: wat");
585 oberon_generate_begin_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
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
;
599 jvm_init_local_object(p
, var
);
605 oberon_generate_end_proc(oberon_context_t
* ctx
)
610 m
= ctx
-> mod
-> gen_mod
;
613 jvm_generate_function_end(p
);
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
;
626 proc
= desig
-> item
.var
;
627 fullname
= jvm_get_field_full_name(proc
);
628 signature
= jvm_get_procedure_signature(proc
-> type
);
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
);
642 jvm_generate(p
, args_cells
, result_cells
, "invokestatic %s%s", fullname
, signature
);
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
);
652 oberon_generate_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
659 m
= ctx
-> mod
-> gen_mod
;
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
);
671 jvm_generate(p
, 0, 0, "return");
676 jvm_push_var(gen_proc_t
* p
, oberon_object_t
* var
)
678 int cell_size
= jvm_cell_size_for_type(var
-> type
);
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
);
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
);
695 jvm_generate_expr_new_static(gen_proc_t
* p
, oberon_type_t
* type
, int num
, oberon_expr_t
* arg
)
698 jvm_generate_new_static(p
, type
);
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
);
709 push_item(gen_proc_t
* p
, oberon_item_t
* item
)
714 jvm_push_var(p
, item
-> var
);
717 jvm_generate_push_int(p
, item
-> integer
);
720 jvm_generate_push_int(p
, item
-> boolean
);
723 jvm_generate_call_proc(p
, (oberon_expr_t
*) item
);
726 gen_error("push_item: TODO index");
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
);
735 /* Все объекты представляются как увказатели */
736 push_item(p
, item
-> parent
);
739 jvm_generate(p
, 0, 1, "aconst_null");
742 jvm_generate_expr_new_pointer(p
, item
-> result
, item
-> num_args
, item
-> args
);
745 jvm_generate_push_float(p
, item
-> real
, item
-> result
-> size
);
748 gen_error("push_item: unk mode %i", item
-> mode
);
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
);
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
);
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
= "";
810 gen_error("jvm_generate_compare_op: wat");
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
);
823 jvm_generate_operator(gen_proc_t
* p
, char prefix
, int op
)
825 int cell_size
= jvm_cell_size_for_prefix(prefix
);
829 jvm_generate(p
, cell_size
, cell_size
, "%cneg", prefix
);
832 jvm_generate_push_int(p
, -1);
833 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cxor", prefix
);
836 jvm_generate_logical_not(p
);
839 jvm_generate_abs(p
, prefix
);
843 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cadd", prefix
);
846 jvm_generate(p
, 2 * cell_size
, cell_size
, "%csub", prefix
);
849 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cmul", prefix
);
852 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cdiv", prefix
);
855 jvm_generate(p
, 2 * cell_size
, cell_size
, "%crem", prefix
);
858 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cand", prefix
);
861 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cxor", prefix
);
864 jvm_generate(p
, 2 * cell_size
, cell_size
, "%cor", prefix
);
873 jvm_generate_compare_op(p
, prefix
, op
);
876 gen_error("jvm_generate_operator: unk op %i", op
);
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 */
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
);
897 jvm_generate_label(p
, label_done
);
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 */
911 jvm_generate(p
, 1, 0, "ifne %s", label_name_false
);
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
);
920 push_operator(gen_proc_t
* p
, oberon_oper_t
* oper
)
922 char prefix
= jvm_get_prefix(oper
-> result
);
930 push_expr(p
, oper
-> left
);
931 jvm_generate_operator(p
, prefix
, op
);
949 push_expr(p
, oper
-> left
);
950 push_expr(p
, oper
-> right
);
951 jvm_generate_operator(p
, prefix
, op
);
955 jvm_generate_logical_or(p
, oper
-> left
, oper
-> right
);
958 jvm_generate_logical_and(p
, oper
-> left
, oper
-> right
);
961 gen_error("push_oper: unk op %i", op
);
967 push_expr(gen_proc_t
* p
, oberon_expr_t
* expr
)
971 push_item(p
, (oberon_item_t
*) expr
);
975 push_operator(p
, (oberon_oper_t
*) expr
);
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
;
989 jvm_generate_store(p
, src
-> result
, item
-> var
-> gen_var
);
992 gen_error("store_expr: TODO index");
995 push_item(p
, item
-> parent
);
997 jvm_generate_store(p
, src
-> result
, item
-> var
-> gen_var
);
1000 gen_error("store_expr: unk mode %i", item
-> mode
);
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);
1015 switch(item -> mode)
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);
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);
1033 gen_error("store_expr: TODO index");
1037 char * fullname = jvm_get_field_full_name(item -> var);
1038 char * desc = jvm_get_descriptor(item -> result);
1039 push_item(p, item -> parent);
1041 jvm_generate(p, 1 + cell_size, 0, "putfield %s %s", fullname, desc);
1045 gen_error("store_expr: unk mode %i", item -> mode);
1052 oberon_generate_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
1057 m
= ctx
-> mod
-> gen_mod
;
1058 p
= m
-> class -> p
;
1060 store_expr(p
, dst
, src
);
1064 oberon_generate_code(oberon_context_t
* ctx
)
1066 printf("generate code\n");
1070 oberon_generator_dump(oberon_context_t
* ctx
, char * path
)
1072 printf("jit: dump code\n");
1076 oberon_generator_get_procedure(oberon_context_t
* ctx
, const char * name
)
1078 printf("jit: get pointer to procedure %s\n", name
);
1083 oberon_generator_get_var(oberon_context_t
* ctx
, const char * name
)
1085 printf("jit: get pointer to var %s\n", name
);