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 case JVM_STORAGE_FRAME
:
261 case JVM_STORAGE_FRAME_VAR
:
262 case JVM_STORAGE_FRAME_PARAM
:
263 case JVM_STORAGE_FRAME_PARAM_VAR
:
264 jvm_generate(p
, 0, 0, ".var %i is '%s' %s from start to end", reg
, name
, desc
);
266 case JVM_STORAGE_REGISTER_VAR
:
267 jvm_generate(p
, 0, 0, ".var %i is '%s' [%s from start to end", reg
, name
, desc
);
269 case JVM_STORAGE_PARAM_VARPTR
:
270 case JVM_STORAGE_FRAME_PARAM_VARPTR
:
271 jvm_generate(p
, 0, 0, ".var %i is '%s' [%s from start to end", reg
, name
, desc
);
272 jvm_generate(p
, 0, 0, ".var %i is '%s$offset' I from start to end", reg
+ 1, name
);
274 case JVM_STORAGE_STATIC
:
275 fprintf(v
-> class -> fp
, ".field public static '%s' %s\n\n", name
, desc
);
277 case JVM_STORAGE_STATIC_VAR
:
278 fprintf(v
-> class -> fp
, ".field public static '%s' [%s\n\n", name
, desc
);
280 case JVM_STORAGE_FIELD
:
281 fprintf(v
-> class -> fp
, ".field public '%s' %s\n\n", name
, desc
);
283 case JVM_STORAGE_FIELD_VAR
:
284 fprintf(v
-> class -> fp
, ".field public '%s' [%s\n\n", name
, desc
);
287 gen_error("jvm_generate_var: invalid storage %i", storage
);
293 jvm_generate_param_initialization(gen_proc_t
* p
, gen_var_t
* v
)
295 int old_reg
= v
-> reg
;
296 int cell_size
= v
-> type
-> cell_size
;
297 char prefix
= v
-> type
-> prefix
;
298 char postfix
= v
-> type
-> postfix
;
299 char * name
= v
-> name
;
300 char * desc
= v
-> type
-> desc
;
302 v
-> reg
= jvm_alloc_register_untyped(p
-> rf
, false);
303 jvm_generate(p
, 0, 0, ".var %i is $%s [%s from start to end", v
-> reg
, name
, desc
);
305 jvm_generate(p
, 0, 1, "iconst_1");
306 jvm_generate(p
, 1, 1, "multianewarray [%s 1", desc
);
307 jvm_generate(p
, 1, 2, "dup");
308 jvm_generate(p
, 1, 0, "astore %i", v
-> reg
);
310 jvm_generate(p
, 0, 1, "iconst_0");
311 jvm_generate(p
, 0, cell_size
, "%cload %i", prefix
, old_reg
);
312 jvm_generate(p
, 1 + 1 + cell_size
, 0, "%castore", postfix
);
316 jvm_generate_var_initialization(gen_proc_t
* p
, gen_var_t
* v
)
318 if(v
-> storage
== JVM_STORAGE_REGISTER_VAR
)
320 jvm_generate(p
, 0, 1, "iconst_1");
321 jvm_generate(p
, 1, 1, "multianewarray [%s 1", v
-> type
-> desc
);
322 jvm_generate(p
, 1, 0, "astore %i", v
-> reg
);
324 else if(v
-> storage
== JVM_STORAGE_STATIC_VAR
)
326 jvm_generate(p
, 0, 1, "iconst_1");
327 jvm_generate(p
, 1, 1, "multianewarray [%s 1", v
-> type
-> desc
);
328 jvm_generate(p
, 1, 0, "putstatic %s [%s", v
-> full_name
, v
-> type
-> desc
);
330 else if(v
-> storage
== JVM_STORAGE_FIELD_VAR
)
332 jvm_generate(p
, 0, 1, "iconst_1");
333 jvm_generate(p
, 1, 1, "multianewarray [%s 1", v
-> type
-> desc
);
334 jvm_generate(p
, 1, 0, "putfield %s [%s", v
-> full_name
, v
-> type
-> desc
);
336 else if(v
-> storage
== JVM_STORAGE_PARAM_VAR
)
338 jvm_generate_param_initialization(p
, v
);
340 else if(v
-> storage
== JVM_STORAGE_FRAME_PARAM
341 || v
-> storage
== JVM_STORAGE_FRAME_PARAM_VAR
)
343 jvm_generate_ldst_prepare(p
, v
);
344 jvm_generate(p
, 0, v
-> type
-> cell_size
, "%cload %i", v
-> type
-> prefix
, v
-> reg
);
345 jvm_generate_store(p
, v
);
347 else if(v
-> storage
== JVM_STORAGE_FRAME_PARAM_VARPTR
)
349 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
350 jvm_generate(p
, 0, 1, "aload %i", v
-> reg
);
351 jvm_generate(p
, 1, 0, "putfield %s [%s", v
-> full_name
, v
-> type
-> desc
);
353 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[v
-> level
]);
354 jvm_generate(p
, 0, 1, "iload %i", v
-> reg
+ 1);
355 jvm_generate(p
, 1, 0, "putfield %s$offset I", v
-> full_name
);
361 jvm_generate_abstract_frame_class()
365 struct gen_class * class;
367 cname = "SYSTEM$FRAME";
368 class = jvm_create_class(cname);
371 fprintf(fp, ".source SYSTEM\n");
372 fprintf(fp, ".class public abstract %s\n", cname);
373 fprintf(fp, ".super java/lang/Object\n\n");
375 fprintf(fp, ".field public $UP L%s;\n\n", cname);
377 fprintf(fp, ".method <init>()V\n");
378 fprintf(fp, " aload_0\n");
379 fprintf(fp, " invokespecial java/lang/Object/<init>()V\n");
380 fprintf(fp, " return\n");
381 fprintf(fp, ".end method\n\n");
383 jvm_destroy_class(class);
387 static struct gen_class
*
388 jvm_generate_frame_class(oberon_object_t
* proc
)
393 struct gen_class
* class;
394 oberon_object_t
* var
;
396 abscname
= "SYSTEM$FRAME";
397 cname
= jvm_get_field_full_name_safe(proc
);
398 cname
= new_string("%s$FRAME", cname
);
399 class = jvm_create_class(proc
-> gen_proc
-> class -> dir
, cname
);
402 class -> full_name
= cname
;
404 fprintf(fp
, ".source %s\n", proc
-> module
-> name
);
405 fprintf(fp
, ".class public %s\n", cname
);
406 fprintf(fp
, ".super %s\n\n", abscname
);
408 var
= proc
-> scope
-> list
-> next
;
411 if(var
-> class == OBERON_CLASS_VAR
412 || var
-> class == OBERON_CLASS_PARAM
413 || var
-> class == OBERON_CLASS_VAR_PARAM
)
415 gen_var_t
* v
= var
-> gen_var
;
416 if(v
-> storage
== JVM_STORAGE_FRAME
417 || v
-> storage
== JVM_STORAGE_FRAME_PARAM
)
419 fprintf(fp
, "; LEVEL %i\n", v
-> level
);
420 fprintf(fp
, ".field public '%s' %s\n\n", v
-> name
, v
-> type
-> desc
);
422 else if(v
-> storage
== JVM_STORAGE_FRAME_VAR
423 || v
-> storage
== JVM_STORAGE_FRAME_PARAM_VAR
)
425 fprintf(fp
, "; LEVEL %i\n", v
-> level
);
426 fprintf(fp
, ".field public '%s' [%s\n\n", v
-> name
, v
-> type
-> desc
);
428 else if(v
-> storage
== JVM_STORAGE_FRAME_PARAM_VARPTR
)
430 fprintf(fp
, "; LEVEL %i\n", v
-> level
);
431 fprintf(fp
, ".field public '%s' [%s\n", v
-> name
, v
-> type
-> desc
);
432 fprintf(fp
, ".field public '%s$offset' I\n\n", v
-> name
);
438 fprintf(fp
, ".method <init>()V\n");
439 fprintf(fp
, " .limit stack 2\n");
440 fprintf(fp
, " aload_0\n");
441 fprintf(fp
, " invokespecial %s/<init>()V\n", abscname
);
443 var
= proc
-> scope
-> list
-> next
;
446 if(var
-> class == OBERON_CLASS_VAR
447 || var
-> class == OBERON_CLASS_PARAM
448 || var
-> class == OBERON_CLASS_VAR_PARAM
)
450 gen_var_t
* v
= var
-> gen_var
;
451 if(v
-> storage
== JVM_STORAGE_FRAME_VAR
452 || v
-> storage
== JVM_STORAGE_FRAME_PARAM_VAR
)
454 fprintf(fp
, " aload_0\n");
455 fprintf(fp
, " iconst_1\n");
456 fprintf(fp
, " multianewarray [%s 1\n", v
-> type
-> desc
);
457 fprintf(fp
, " putfield %s [%s\n", v
-> full_name
, v
-> type
-> desc
);
463 fprintf(fp
, " return\n");
464 fprintf(fp
, ".end method\n\n");
466 jvm_destroy_class(class);
472 jvm_generate_procedure_frame(oberon_object_t
* proc
)
477 struct gen_class
* class;
479 p
= proc
-> gen_proc
;
481 // jvm_generate_abstract_frame_class();
483 class = jvm_generate_frame_class(proc
);
485 t
= GC_MALLOC(sizeof *t
);
486 memset(t
, 0, sizeof *t
);
492 t
-> desc
= new_string("L%s;", class -> full_name
);
494 v
= jvm_create_function_var(p
, JVM_STORAGE_REGISTER
, "$FP$", t
);
496 p
-> reg_frame
[p
-> level
] = v
-> reg
;
500 jvm_generate_frame_initialization(gen_proc_t
* p
)
502 jvm_generate_var(p
-> frame_v
);
503 jvm_generate_ldst_prepare(p
, p
-> frame_v
);
504 jvm_generate(p
, 0, 1, "new %s", p
-> frame_v
-> type
-> class -> full_name
);
505 jvm_generate(p
, 1, 2, "dup");
506 jvm_generate(p
, 1, 0, "invokespecial %s/<init>()V", p
-> frame_v
-> type
-> class -> full_name
);
507 jvm_generate_store(p
, p
-> frame_v
);
511 jvm_get_procedure_signature(oberon_type_t
* ps
)
516 oberon_object_t
* arg
;
518 signature
= new_string("(");
520 num
= ps
-> num_decl
;
522 for(int i
= 0; i
< num
; i
++)
524 desc
= jvm_get_descriptor(arg
-> type
);
525 if(arg
-> class == OBERON_CLASS_VAR_PARAM
)
527 signature
= new_string("%s[%sI", signature
, desc
);
531 signature
= new_string("%s%s", signature
, desc
);
536 desc
= jvm_get_descriptor(ps
-> base
);
537 signature
= new_string("%s)%s", signature
, desc
);
543 jvm_get_frame_descriptor(oberon_object_t
* proc
)
545 char * full_name
= jvm_get_field_full_name_safe(proc
);
546 return new_string("L%s$FRAME;", full_name
);
550 jvm_get_oberon_signature_static_links(oberon_object_t
* parent
)
557 char * up
= jvm_get_oberon_signature_static_links(parent
-> parent
);
558 char * desc
= jvm_get_frame_descriptor(parent
);
559 return new_string("%s%s", up
, desc
);
563 jvm_get_local_procedure_signature(oberon_object_t
* proc
)
569 oberon_object_t
* arg
;
574 signature
= new_string("(");
578 stlinks
= jvm_get_oberon_signature_static_links(proc
-> parent
);
579 signature
= new_string("%s%s", signature
, stlinks
);
582 num
= ps
-> num_decl
;
584 for(int i
= 0; i
< num
; i
++)
586 desc
= jvm_get_descriptor(arg
-> type
);
587 if(arg
-> class == OBERON_CLASS_VAR_PARAM
)
589 signature
= new_string("%s[%sI", signature
, desc
);
593 signature
= new_string("%s%s", signature
, desc
);
598 desc
= jvm_get_descriptor(ps
-> base
);
599 signature
= new_string("%s)%s", signature
, desc
);
606 jvm_generate_staticlinks_recursive(gen_proc_t
* p
, oberon_object_t
* parent
, int level
)
613 jvm_generate_staticlinks_recursive(p
, parent
-> parent
, level
- 1);
614 int reg
= p
-> reg_frame
[level
];
615 char * desc
= jvm_get_frame_descriptor(parent
);
616 jvm_generate(p
, 0, 0, ".var %i is SL$%i %s from start to end", reg
, level
, desc
);
620 jvm_generate_staticlinks(oberon_object_t
* proc
)
624 p
= proc
-> gen_proc
;
626 jvm_generate_staticlinks_recursive(p
, proc
-> parent
, p
-> level
);
630 jvm_create_staticlinks_recursive(gen_proc_t
* p
, oberon_object_t
* parent
, int level
)
637 jvm_create_staticlinks_recursive(p
, parent
-> parent
, level
- 1);
638 p
-> reg_frame
[level
] = jvm_alloc_register_untyped(p
-> rf
, false);
642 jvm_create_static_links(oberon_object_t
* proc
)
645 p
= proc
-> gen_proc
;
648 oberon_object_t
* prc
= proc
;
655 size_t sz
= sizeof(*p
-> reg_frame
) * max_frames
;
656 p
-> max_frames
= max_frames
;
657 p
-> reg_frame
= GC_MALLOC(sz
);
658 memset(p
-> reg_frame
, 0, sz
);
660 p
-> level
= max_frames
- 1;
661 jvm_create_staticlinks_recursive(p
, proc
-> parent
, p
-> level
);
665 jvm_generate_push_static_links_recursive(gen_proc_t
* p
, oberon_object_t
* parent
)
672 jvm_generate_push_static_links_recursive(p
, parent
-> parent
);
673 int level
= parent
-> gen_proc
-> level
;
674 jvm_generate(p
, 0, 1, "aload %i", p
-> reg_frame
[level
]);
678 jvm_generate_push_static_links(gen_proc_t
* p
, oberon_object_t
* proc
)
680 jvm_generate_push_static_links_recursive(p
, proc
-> parent
);