DEADSOFTWARE

Исправлена конверсия строки единичного размера в символ в сравнениях
[dsw-obn.git] / src / backends / jvm / generator-jvm-abi.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-abi.h"
15 #include "generator-jvm-asm.h"
16 #include "generator-jvm-basic.h"
18 void
19 jvm_generate_ldst_prepare(gen_proc_t * p, gen_var_t * v)
20 {
21 char * desc = v -> type -> desc;
22 char * full_name = v -> full_name;
23 switch(v -> storage)
24 {
25 case JVM_STORAGE_REGISTER:
26 case JVM_STORAGE_STATIC:
27 case JVM_STORAGE_FIELD:
28 case JVM_STORAGE_PARAM:
29 break;
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");
34 break;
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");
38 break;
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");
42 break;
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);
46 break;
47 case JVM_STORAGE_FRAME:
48 case JVM_STORAGE_FRAME_PARAM:
49 jvm_generate(p, 0, 1, "aload %i", p -> reg_frame[v -> level]);
50 break;
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");
56 break;
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);
62 break;
63 default:
64 gen_error("jvm_abi_obn_generate_ldst_prepare: wat %i", v -> storage);
65 }
66 }
68 void
69 jvm_generate_load(gen_proc_t * p, gen_var_t * src)
70 {
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)
78 {
79 case JVM_STORAGE_REGISTER:
80 case JVM_STORAGE_PARAM:
81 jvm_generate(p, 0, cell_size, "%cload %i", prefix, src -> reg);
82 break;
83 case JVM_STORAGE_STATIC:
84 jvm_generate(p, 0, cell_size, "getstatic %s %s", full_name, desc);
85 break;
86 case JVM_STORAGE_FIELD:
87 jvm_generate(p, 0, cell_size, "getfield %s %s", full_name, desc);
88 break;
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);
99 break;
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);
104 break;
105 default:
106 gen_error("jvm_generate_load: unknow storage type %i", src -> storage);
107 break;
111 void
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);
125 break;
126 case JVM_STORAGE_STATIC:
127 jvm_generate(p, 0, cell_size, "putstatic %s %s", full_name, desc);
128 break;
129 case JVM_STORAGE_FIELD:
130 jvm_generate(p, 0, cell_size, "putfield %s %s", full_name, desc);
131 break;
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);
141 break;
142 case JVM_STORAGE_FRAME:
143 case JVM_STORAGE_FRAME_PARAM:
144 jvm_generate(p, 0, cell_size, "putfield %s %s", full_name, desc);
145 break;
146 default:
147 gen_error("jvm_generate_store: unknow storage type %i", dst -> storage);
148 break;
152 static gen_var_t *
153 jvm_new_var()
155 gen_var_t * v = GC_MALLOC(sizeof *v);
156 memset(v, 0, sizeof *v);
157 return v;
160 gen_var_t *
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);
166 v -> type = t;
167 v -> class = class;
169 assert(class -> p == NULL);
170 switch(storage)
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);
177 break;
178 default:
179 gen_error("jvm_create_class_var: invalid storage %i", storage);
180 break;
183 return v;
186 gen_var_t *
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);
192 v -> type = t;
193 v -> p = p;
194 v -> level = p -> level;
196 switch(storage)
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);
202 break;
203 case JVM_STORAGE_PARAM_VARPTR:
204 /* Содержит пару массив+индекс */
205 v -> reg = jvm_alloc_register_untyped(p -> rf, true);
206 break;
207 case JVM_STORAGE_REGISTER_VAR:
208 /* Содержит массив */
209 v -> reg = jvm_alloc_register_untyped(p -> rf, false);
210 break;
211 case JVM_STORAGE_FRAME:
212 case JVM_STORAGE_FRAME_VAR:
213 v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
214 break;
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);
219 break;
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);
224 break;
225 default:
226 gen_error("jvm_create_class_var: invalid storage %i", storage);
227 break;
230 return v;
233 gen_var_t *
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);
240 v -> type = t;
241 v -> p = p;
242 v -> level = p -> level;
243 return v;
246 void
247 jvm_generate_var(gen_var_t * v)
249 enum gen_storage storage = v -> storage;
250 int reg = v -> reg;
251 char * name = v -> name;
252 char * desc = v -> type -> desc;
253 gen_proc_t * p = v -> p;
255 switch(v -> storage)
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);
265 break;
266 case JVM_STORAGE_REGISTER_VAR:
267 jvm_generate(p, 0, 0, ".var %i is '%s' [%s from start to end", reg, name, desc);
268 break;
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);
273 break;
274 case JVM_STORAGE_STATIC:
275 fprintf(v -> class -> fp, ".field public static '%s' %s\n\n", name, desc);
276 break;
277 case JVM_STORAGE_STATIC_VAR:
278 fprintf(v -> class -> fp, ".field public static '%s' [%s\n\n", name, desc);
279 break;
280 case JVM_STORAGE_FIELD:
281 fprintf(v -> class -> fp, ".field public '%s' %s\n\n", name, desc);
282 break;
283 case JVM_STORAGE_FIELD_VAR:
284 fprintf(v -> class -> fp, ".field public '%s' [%s\n\n", name, desc);
285 break;
286 default:
287 gen_error("jvm_generate_var: invalid storage %i", storage);
288 break;
292 static void
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);
315 void
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);
359 /*
360 static void
361 jvm_generate_abstract_frame_class()
363 FILE * fp;
364 char * cname;
365 struct gen_class * class;
367 cname = "SYSTEM$FRAME";
368 class = jvm_create_class(cname);
369 fp = class -> fp;
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);
385 */
387 static struct gen_class *
388 jvm_generate_frame_class(oberon_object_t * proc)
390 FILE * fp;
391 char * cname;
392 char * abscname;
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);
400 fp = class -> fp;
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;
409 while(var)
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);
435 var = var -> next;
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;
444 while(var)
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);
460 var = var -> next;
463 fprintf(fp, " return\n");
464 fprintf(fp, ".end method\n\n");
466 jvm_destroy_class(class);
468 return class;
471 void
472 jvm_generate_procedure_frame(oberon_object_t * proc)
474 gen_proc_t * p;
475 gen_type_t * t;
476 gen_var_t * v;
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);
487 t -> class = class;
488 t -> wide = false;
489 t -> prefix = 'a';
490 t -> postfix = 'a';
491 t -> cell_size = 1;
492 t -> desc = new_string("L%s;", class -> full_name);
494 v = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "$FP$", t);
495 p -> frame_v = v;
496 p -> reg_frame[p -> level] = v -> reg;
499 void
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);
510 char *
511 jvm_get_procedure_signature(oberon_type_t * ps)
513 char * signature;
514 char * desc;
515 int num;
516 oberon_object_t * arg;
518 signature = new_string("(");
520 num = ps -> num_decl;
521 arg = ps -> 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);
529 else
531 signature = new_string("%s%s", signature, desc);
533 arg = arg -> next;
536 desc = jvm_get_descriptor(ps -> base);
537 signature = new_string("%s)%s", signature, desc);
539 return signature;
542 static char *
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);
549 static char *
550 jvm_get_oberon_signature_static_links(oberon_object_t * parent)
552 if(parent == NULL)
554 return "";
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);
562 char *
563 jvm_get_local_procedure_signature(oberon_object_t * proc)
565 char * stlinks;
566 char * signature;
567 char * desc;
568 int num;
569 oberon_object_t * arg;
570 oberon_type_t * ps;
572 ps = proc -> type;
574 signature = new_string("(");
576 if(proc -> local)
578 stlinks = jvm_get_oberon_signature_static_links(proc -> parent);
579 signature = new_string("%s%s", signature, stlinks);
582 num = ps -> num_decl;
583 arg = ps -> 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);
591 else
593 signature = new_string("%s%s", signature, desc);
595 arg = arg -> next;
598 desc = jvm_get_descriptor(ps -> base);
599 signature = new_string("%s)%s", signature, desc);
601 return signature;
605 static void
606 jvm_generate_staticlinks_recursive(gen_proc_t * p, oberon_object_t * parent, int level)
608 if(parent == NULL)
610 return;
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);
619 void
620 jvm_generate_staticlinks(oberon_object_t * proc)
622 gen_proc_t * p;
624 p = proc -> gen_proc;
626 jvm_generate_staticlinks_recursive(p, proc -> parent, p -> level);
629 static void
630 jvm_create_staticlinks_recursive(gen_proc_t * p, oberon_object_t * parent, int level)
632 if(parent == NULL)
634 return;
637 jvm_create_staticlinks_recursive(p, parent -> parent, level - 1);
638 p -> reg_frame[level] = jvm_alloc_register_untyped(p -> rf, false);
641 void
642 jvm_create_static_links(oberon_object_t * proc)
644 gen_proc_t * p;
645 p = proc -> gen_proc;
647 int max_frames = 0;
648 oberon_object_t * prc = proc;
649 while(prc)
651 max_frames += 1;
652 prc = prc -> parent;
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);
664 static void
665 jvm_generate_push_static_links_recursive(gen_proc_t * p, oberon_object_t * parent)
667 if(parent == NULL)
669 return;
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]);
677 void
678 jvm_generate_push_static_links(gen_proc_t * p, oberon_object_t * proc)
680 jvm_generate_push_static_links_recursive(p, proc -> parent);