11 #include "../../../include/oberon.h"
12 #include "../../oberon-internals.h"
13 #include "generator-jvm.h"
14 #include "generator-jvm-abi.h"
15 #include "generator-jvm-asm.h"
16 #include "generator-jvm-basic.h"
19 jvm_generate_ldst_prepare(gen_proc_t
* p
, gen_var_t
* v
)
21 char * desc
= v
-> type
-> desc
;
22 char * full_name
= v
-> full_name
;
25 case JVM_STORAGE_REGISTER
:
26 case JVM_STORAGE_STATIC
:
27 case JVM_STORAGE_FIELD
:
28 case JVM_STORAGE_PARAM
:
30 case JVM_STORAGE_REGISTER_VAR
:
31 case JVM_STORAGE_PARAM_VAR
:
32 jvm_generate(p
, 0, 1, "aload %i", v
-> reg
);
33 jvm_generate(p
, 0, 1, "iconst_0");
35 case JVM_STORAGE_STATIC_VAR
:
36 jvm_generate(p
, 0, 1, "getstatic %s [%s", full_name
, desc
);
37 jvm_generate(p
, 0, 1, "iconst_0");
39 case JVM_STORAGE_FIELD_VAR
:
40 jvm_generate(p
, 1, 1, "getfield %s [%s", full_name
, desc
);
41 jvm_generate(p
, 0, 1, "iconst_0");
43 case JVM_STORAGE_PARAM_VARPTR
:
44 jvm_generate(p
, 0, 1, "aload %i", v
-> reg
);
45 jvm_generate(p
, 0, 1, "iload %i", v
-> reg
+ 1);
47 case JVM_STORAGE_FRAME
:
48 case JVM_STORAGE_FRAME_PARAM
:
49 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
51 case JVM_STORAGE_FRAME_VAR
:
52 case JVM_STORAGE_FRAME_PARAM_VAR
:
53 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
54 jvm_generate(p
, 1, 1, "getfield %s [%s", full_name
, desc
);
55 jvm_generate(p
, 0, 1, "iconst_0");
57 case JVM_STORAGE_FRAME_PARAM_VARPTR
:
58 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
59 jvm_generate(p
, 1, 1, "getfield %s [%s", full_name
, desc
);
60 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
61 jvm_generate(p
, 1, 1, "getfield %s$offset I", full_name
);
64 gen_error("jvm_abi_obn_generate_ldst_prepare: wat %i", v
-> storage
);
69 jvm_generate_load(gen_proc_t
* p
, gen_var_t
* src
)
71 char prefix
= src
-> type
-> prefix
;
72 char postfix
= src
-> type
-> postfix
;
73 int cell_size
= src
-> type
-> cell_size
;
74 char * full_name
= src
-> full_name
;
75 char * desc
= src
-> type
-> desc
;
77 switch(src
-> storage
)
79 case JVM_STORAGE_REGISTER
:
80 case JVM_STORAGE_PARAM
:
81 jvm_generate(p
, 0, cell_size
, "%cload %i", prefix
, src
-> reg
);
83 case JVM_STORAGE_STATIC
:
84 jvm_generate(p
, 0, cell_size
, "getstatic %s %s", full_name
, desc
);
86 case JVM_STORAGE_FIELD
:
87 jvm_generate(p
, 0, cell_size
, "getfield %s %s", full_name
, desc
);
89 case JVM_STORAGE_REGISTER_VAR
:
90 case JVM_STORAGE_STATIC_VAR
:
91 case JVM_STORAGE_FIELD_VAR
:
92 case JVM_STORAGE_PARAM_VAR
:
93 case JVM_STORAGE_PARAM_VARPTR
:
94 case JVM_STORAGE_FRAME_VAR
:
95 case JVM_STORAGE_FRAME_PARAM_VAR
:
96 case JVM_STORAGE_FRAME_PARAM_VARPTR
:
97 jvm_generate_ldst_prepare(p
, src
);
98 jvm_generate(p
, 1 + 1, cell_size
, "%caload", postfix
);
100 case JVM_STORAGE_FRAME
:
101 case JVM_STORAGE_FRAME_PARAM
:
102 jvm_generate_ldst_prepare(p
, src
);
103 jvm_generate(p
, 1, cell_size
, "getfield %s %s", full_name
, desc
);
106 gen_error("jvm_generate_load: unknow storage type %i", src
-> storage
);
112 jvm_generate_store(gen_proc_t
* p
, gen_var_t
* dst
)
114 char prefix
= dst
-> type
-> prefix
;
115 char postfix
= dst
-> type
-> postfix
;
116 int cell_size
= dst
-> type
-> cell_size
;
117 char * full_name
= dst
-> full_name
;
118 char * desc
= dst
-> type
-> desc
;
120 switch(dst
-> storage
)
122 case JVM_STORAGE_REGISTER
:
123 case JVM_STORAGE_PARAM
:
124 jvm_generate(p
, cell_size
, 0, "%cstore %i", prefix
, dst
-> reg
);
126 case JVM_STORAGE_STATIC
:
127 jvm_generate(p
, 0, cell_size
, "putstatic %s %s", full_name
, desc
);
129 case JVM_STORAGE_FIELD
:
130 jvm_generate(p
, 0, cell_size
, "putfield %s %s", full_name
, desc
);
132 case JVM_STORAGE_REGISTER_VAR
:
133 case JVM_STORAGE_STATIC_VAR
:
134 case JVM_STORAGE_FIELD_VAR
:
135 case JVM_STORAGE_PARAM_VAR
:
136 case JVM_STORAGE_PARAM_VARPTR
:
137 case JVM_STORAGE_FRAME_VAR
:
138 case JVM_STORAGE_FRAME_PARAM_VAR
:
139 case JVM_STORAGE_FRAME_PARAM_VARPTR
:
140 jvm_generate(p
, 1 + 1 + cell_size
, 0, "%castore", postfix
);
142 case JVM_STORAGE_FRAME
:
143 case JVM_STORAGE_FRAME_PARAM
:
144 jvm_generate(p
, 0, cell_size
, "putfield %s %s", full_name
, desc
);
147 gen_error("jvm_generate_store: unknow storage type %i", dst
-> storage
);
155 gen_var_t
* v
= GC_MALLOC(sizeof *v
);
156 memset(v
, 0, sizeof *v
);
161 jvm_create_class_var(struct gen_class
* class, enum gen_storage storage
, char * name
, gen_type_t
* t
)
163 gen_var_t
* v
= jvm_new_var();
164 v
-> storage
= storage
;
165 v
-> name
= new_string(name
);
169 assert(class -> p
== NULL
);
172 case JVM_STORAGE_STATIC
:
173 case JVM_STORAGE_FIELD
:
174 case JVM_STORAGE_STATIC_VAR
:
175 case JVM_STORAGE_FIELD_VAR
:
176 v
-> full_name
= new_string("%s/%s", class -> full_name
, name
);
179 gen_error("jvm_create_class_var: invalid storage %i", storage
);
187 jvm_create_function_var(gen_proc_t
* p
, enum gen_storage storage
, char * name
, gen_type_t
* t
)
189 gen_var_t
* v
= jvm_new_var();
190 v
-> storage
= storage
;
191 v
-> name
= new_string(name
);
194 v
-> level
= p
-> level
;
198 case JVM_STORAGE_REGISTER
:
199 case JVM_STORAGE_PARAM
:
200 case JVM_STORAGE_PARAM_VAR
:
201 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, t
-> wide
);
203 case JVM_STORAGE_PARAM_VARPTR
:
204 /* Содержит пару массив+индекс */
205 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, true);
207 case JVM_STORAGE_REGISTER_VAR
:
208 /* Содержит массив */
209 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, false);
211 case JVM_STORAGE_FRAME
:
212 case JVM_STORAGE_FRAME_VAR
:
213 v
-> full_name
= new_string("%s$FRAME/%s", p
-> full_name
, name
);
215 case JVM_STORAGE_FRAME_PARAM
:
216 case JVM_STORAGE_FRAME_PARAM_VAR
:
217 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, t
-> wide
);
218 v
-> full_name
= new_string("%s$FRAME/%s", p
-> full_name
, name
);
220 case JVM_STORAGE_FRAME_PARAM_VARPTR
:
221 /* Содержит пару массив+индекс */
222 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, true);
223 v
-> full_name
= new_string("%s$FRAME/%s", p
-> full_name
, name
);
226 gen_error("jvm_create_class_var: invalid storage %i", storage
);
234 jvm_create_function_temp_var(gen_proc_t
* p
, gen_type_t
* t
)
236 gen_var_t
* v
= jvm_new_var();
237 v
-> storage
= JVM_STORAGE_REGISTER
;
238 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, t
-> wide
);
239 v
-> name
= new_string("$TEMP_%i$", v
-> reg
);
242 v
-> level
= p
-> level
;
247 jvm_generate_var(gen_var_t
* v
)
249 enum gen_storage storage
= v
-> storage
;
251 char * name
= v
-> name
;
252 char * desc
= v
-> type
-> desc
;
253 gen_proc_t
* p
= v
-> p
;
257 case JVM_STORAGE_REGISTER
:
258 case JVM_STORAGE_PARAM
:
259 case JVM_STORAGE_PARAM_VAR
:
260 jvm_generate(p
, 0, 0, ".var %i is %s %s from start to end", reg
, name
, desc
);
262 case JVM_STORAGE_REGISTER_VAR
:
263 jvm_generate(p
, 0, 0, ".var %i is %s [%s from start to end", reg
, name
, desc
);
265 case JVM_STORAGE_PARAM_VARPTR
:
266 jvm_generate(p
, 0, 0, ".var %i is %s [%s from start to end", reg
, name
, desc
);
267 jvm_generate(p
, 0, 0, ".var %i is %s$offset I from start to end", reg
+ 1, name
);
269 case JVM_STORAGE_STATIC
:
270 fprintf(v
-> class -> fp
, ".field public static %s %s\n\n", name
, desc
);
272 case JVM_STORAGE_STATIC_VAR
:
273 fprintf(v
-> class -> fp
, ".field public static %s [%s\n\n", name
, desc
);
275 case JVM_STORAGE_FIELD
:
276 fprintf(v
-> class -> fp
, ".field public %s %s\n\n", name
, desc
);
278 case JVM_STORAGE_FIELD_VAR
:
279 fprintf(v
-> class -> fp
, ".field public %s [%s\n\n", name
, desc
);
281 case JVM_STORAGE_FRAME
:
282 case JVM_STORAGE_FRAME_VAR
:
283 case JVM_STORAGE_FRAME_PARAM
:
284 case JVM_STORAGE_FRAME_PARAM_VAR
:
285 case JVM_STORAGE_FRAME_PARAM_VARPTR
:
286 /* См. ниже jvm_generate_frame_class() */
289 gen_error("jvm_generate_var: invalid storage %i", storage
);
295 jvm_generate_param_initialization(gen_proc_t
* p
, gen_var_t
* v
)
297 int old_reg
= v
-> reg
;
298 int cell_size
= v
-> type
-> cell_size
;
299 char prefix
= v
-> type
-> prefix
;
300 char postfix
= v
-> type
-> postfix
;
301 char * name
= v
-> name
;
302 char * desc
= v
-> type
-> desc
;
304 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, false);
305 jvm_generate(p
, 0, 0, ".var %i is $%s [%s from start to end", v
-> reg
, name
, desc
);
307 jvm_generate(p
, 0, 1, "iconst_1");
308 jvm_generate(p
, 1, 1, "multianewarray [%s 1", desc
);
309 jvm_generate(p
, 1, 2, "dup");
310 jvm_generate(p
, 1, 0, "astore %i", v
-> reg
);
312 jvm_generate(p
, 0, 1, "iconst_0");
313 jvm_generate(p
, 0, cell_size
, "%cload %i", prefix
, old_reg
);
314 jvm_generate(p
, 1 + 1 + cell_size
, 0, "%castore", postfix
);
318 jvm_generate_var_initialization(gen_proc_t
* p
, gen_var_t
* v
)
320 if(v
-> storage
== JVM_STORAGE_REGISTER_VAR
)
322 jvm_generate(p
, 0, 1, "iconst_1");
323 jvm_generate(p
, 1, 1, "multianewarray [%s 1", v
-> type
-> desc
);
324 jvm_generate(p
, 1, 0, "astore %i", v
-> reg
);
326 else if(v
-> storage
== JVM_STORAGE_STATIC_VAR
)
328 jvm_generate(p
, 0, 1, "iconst_1");
329 jvm_generate(p
, 1, 1, "multianewarray [%s 1", v
-> type
-> desc
);
330 jvm_generate(p
, 1, 0, "putstatic %s [%s", v
-> full_name
, v
-> type
-> desc
);
332 else if(v
-> storage
== JVM_STORAGE_FIELD_VAR
)
334 jvm_generate(p
, 0, 1, "iconst_1");
335 jvm_generate(p
, 1, 1, "multianewarray [%s 1", v
-> type
-> desc
);
336 jvm_generate(p
, 1, 0, "putfield %s [%s", v
-> full_name
, v
-> type
-> desc
);
338 else if(v
-> storage
== JVM_STORAGE_PARAM_VAR
)
340 jvm_generate_param_initialization(p
, v
);
342 else if(v
-> storage
== JVM_STORAGE_FRAME_PARAM
343 || v
-> storage
== JVM_STORAGE_FRAME_PARAM_VAR
)
345 jvm_generate_ldst_prepare(p
, v
);
346 jvm_generate(p
, 0, v
-> type
-> cell_size
, "%cload %i", v
-> type
-> prefix
, v
-> reg
);
347 jvm_generate_store(p
, v
);
349 else if(v
-> storage
== JVM_STORAGE_FRAME_PARAM_VARPTR
)
351 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
352 jvm_generate(p
, 0, 1, "aload %i", v
-> reg
);
353 jvm_generate(p
, 1, 0, "putfield %s [%s", v
-> full_name
, v
-> type
-> desc
);
355 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
356 jvm_generate(p
, 0, 1, "iload %i", v
-> reg
+ 1);
357 jvm_generate(p
, 1, 0, "putfield %s$offset I", v
-> full_name
);
363 jvm_generate_abstract_frame_class()
367 struct gen_class * class;
369 cname = "SYSTEM$FRAME";
370 class = jvm_create_class(cname);
373 fprintf(fp, ".source SYSTEM\n");
374 fprintf(fp, ".class public abstract %s\n", cname);
375 fprintf(fp, ".super java/lang/Object\n\n");
377 fprintf(fp, ".field public $UP L%s;\n\n", cname);
379 fprintf(fp, ".method <init>()V\n");
380 fprintf(fp, " aload_0\n");
381 fprintf(fp, " invokespecial java/lang/Object/<init>()V\n");
382 fprintf(fp, " return\n");
383 fprintf(fp, ".end method\n\n");
385 jvm_destroy_class(class);
389 static struct gen_class
*
390 jvm_generate_frame_class(oberon_object_t
* proc
)
395 struct gen_class
* class;
396 oberon_object_t
* var
;
398 abscname
= "SYSTEM$FRAME";
399 cname
= jvm_get_field_full_name_safe(proc
);
400 cname
= new_string("%s$FRAME", cname
);
401 class = jvm_create_class(cname
);
404 class -> full_name
= cname
;
406 fprintf(fp
, ".source %s\n", proc
-> module
-> name
);
407 fprintf(fp
, ".class public %s\n", cname
);
408 fprintf(fp
, ".super %s\n\n", abscname
);
410 var
= proc
-> scope
-> list
-> next
;
413 if(var
-> class == OBERON_CLASS_VAR
414 || var
-> class == OBERON_CLASS_PARAM
415 || var
-> class == OBERON_CLASS_VAR_PARAM
)
417 gen_var_t
* v
= var
-> gen_var
;
418 if(v
-> storage
== JVM_STORAGE_FRAME
419 || v
-> storage
== JVM_STORAGE_FRAME_PARAM
)
421 fprintf(fp
, "; LEVEL %i\n", v
-> level
);
422 fprintf(fp
, ".field public %s %s\n\n", v
-> name
, v
-> type
-> desc
);
424 else if(v
-> storage
== JVM_STORAGE_FRAME_VAR
425 || v
-> storage
== JVM_STORAGE_FRAME_PARAM_VAR
)
427 fprintf(fp
, "; LEVEL %i\n", v
-> level
);
428 fprintf(fp
, ".field public %s [%s\n\n", v
-> name
, v
-> type
-> desc
);
430 else if(v
-> storage
== JVM_STORAGE_FRAME_PARAM_VARPTR
)
432 fprintf(fp
, "; LEVEL %i\n", v
-> level
);
433 fprintf(fp
, ".field public %s [%s\n", v
-> name
, v
-> type
-> desc
);
434 fprintf(fp
, ".field public %s$offset I\n\n", v
-> name
);
440 fprintf(fp
, ".method <init>()V\n");
441 fprintf(fp
, " .limit stack 2\n");
442 fprintf(fp
, " aload_0\n");
443 fprintf(fp
, " invokespecial %s/<init>()V\n", abscname
);
445 var
= proc
-> scope
-> list
-> next
;
448 if(var
-> class == OBERON_CLASS_VAR
449 || var
-> class == OBERON_CLASS_PARAM
450 || var
-> class == OBERON_CLASS_VAR_PARAM
)
452 gen_var_t
* v
= var
-> gen_var
;
453 if(v
-> storage
== JVM_STORAGE_FRAME_VAR
454 || v
-> storage
== JVM_STORAGE_FRAME_PARAM_VAR
)
456 fprintf(fp
, " aload_0\n");
457 fprintf(fp
, " iconst_1\n");
458 fprintf(fp
, " multianewarray [%s 1\n", v
-> type
-> desc
);
459 fprintf(fp
, " putfield %s [%s\n", v
-> full_name
, v
-> type
-> desc
);
465 fprintf(fp
, " return\n");
466 fprintf(fp
, ".end method\n\n");
468 jvm_destroy_class(class);
474 jvm_generate_procedure_frame(oberon_object_t
* proc
)
479 struct gen_class
* class;
481 p
= proc
-> gen_proc
;
483 // jvm_generate_abstract_frame_class();
485 class = jvm_generate_frame_class(proc
);
487 t
= GC_MALLOC(sizeof *t
);
488 memset(t
, 0, sizeof *t
);
494 t
-> desc
= new_string("L%s;", class -> full_name
);
496 v
= jvm_create_function_var(p
, JVM_STORAGE_REGISTER
, "$FP$", t
);
498 printf("jvm_generate_procedure_frame: %p level %i\n", p
-> reg_frame
, p
-> level
);
499 p
-> reg_frame
[p
-> level
] = v
-> reg
;
503 jvm_generate_frame_initialization(gen_proc_t
* p
)
505 jvm_generate_var(p
-> frame_v
);
506 jvm_generate_ldst_prepare(p
, p
-> frame_v
);
507 jvm_generate(p
, 0, 1, "new %s", p
-> frame_v
-> type
-> class -> full_name
);
508 jvm_generate(p
, 1, 2, "dup");
509 jvm_generate(p
, 1, 0, "invokespecial %s/<init>()V", p
-> frame_v
-> type
-> class -> full_name
);
510 jvm_generate_store(p
, p
-> frame_v
);
514 jvm_get_procedure_signature(oberon_type_t
* ps
)
519 oberon_object_t
* arg
;
521 signature
= new_string("(");
523 num
= ps
-> num_decl
;
525 for(int i
= 0; i
< num
; i
++)
527 desc
= jvm_get_descriptor(arg
-> type
);
528 if(arg
-> class == OBERON_CLASS_VAR_PARAM
)
530 signature
= new_string("%s[%sI", signature
, desc
);
534 signature
= new_string("%s%s", signature
, desc
);
539 desc
= jvm_get_descriptor(ps
-> base
);
540 signature
= new_string("%s)%s", signature
, desc
);
546 jvm_get_frame_descriptor(oberon_object_t
* proc
)
548 char * full_name
= jvm_get_field_full_name_safe(proc
);
549 return new_string("L%s$FRAME;", full_name
);
553 jvm_get_oberon_signature_static_links(oberon_object_t
* parent
)
560 char * up
= jvm_get_oberon_signature_static_links(parent
-> parent
);
561 char * desc
= jvm_get_frame_descriptor(parent
);
562 return new_string("%s%s", up
, desc
);
566 jvm_get_local_procedure_signature(oberon_object_t
* proc
)
572 oberon_object_t
* arg
;
577 signature
= new_string("(");
581 stlinks
= jvm_get_oberon_signature_static_links(proc
-> parent
);
582 signature
= new_string("%s%s", signature
, stlinks
);
585 num
= ps
-> num_decl
;
587 for(int i
= 0; i
< num
; i
++)
589 desc
= jvm_get_descriptor(arg
-> type
);
590 if(arg
-> class == OBERON_CLASS_VAR_PARAM
)
592 signature
= new_string("%s[%sI", signature
, desc
);
596 signature
= new_string("%s%s", signature
, desc
);
601 desc
= jvm_get_descriptor(ps
-> base
);
602 signature
= new_string("%s)%s", signature
, desc
);
609 jvm_generate_staticlinks_recursive(gen_proc_t
* p
, oberon_object_t
* parent
, int level
)
616 jvm_generate_staticlinks_recursive(p
, parent
-> parent
, level
- 1);
617 int reg
= p
-> reg_frame
[level
];
618 char * desc
= jvm_get_frame_descriptor(parent
);
619 jvm_generate(p
, 0, 0, ".var %i is SL$%i %s from start to end", reg
, level
, desc
);
623 jvm_generate_staticlinks(oberon_object_t
* proc
)
627 p
= proc
-> gen_proc
;
629 jvm_generate_staticlinks_recursive(p
, proc
-> parent
, p
-> level
);
633 jvm_create_staticlinks_recursive(gen_proc_t
* p
, oberon_object_t
* parent
, int level
)
640 jvm_create_staticlinks_recursive(p
, parent
-> parent
, level
- 1);
641 p
-> reg_frame
[level
] = jvm_alloc_register_untyped(p
-> rf
, false);
645 jvm_create_static_links(oberon_object_t
* proc
)
648 p
= proc
-> gen_proc
;
651 oberon_object_t
* prc
= proc
;
658 p
-> max_frames
= max_frames
;
659 p
-> reg_frame
= GC_MALLOC(sizeof *p
-> reg_frame
);
660 memset(p
-> reg_frame
, 0, max_frames
* sizeof *p
-> reg_frame
);
662 p
-> level
= max_frames
- 1;
663 jvm_create_staticlinks_recursive(p
, proc
-> parent
, p
-> level
);
667 jvm_generate_push_static_links_recursive(gen_proc_t
* p
, oberon_object_t
* parent
)
674 jvm_generate_push_static_links_recursive(p
, parent
-> parent
);
675 int level
= parent
-> gen_proc
-> level
;
676 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[level
]);
680 jvm_generate_push_static_links(gen_proc_t
* p
, oberon_object_t
* proc
)
682 jvm_generate_push_static_links_recursive(p
, proc
-> parent
);