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_create_proc(struct gen_class
* class)
21 gen_proc_t
* p
= GC_MALLOC(sizeof *p
);
22 memset(p
, 0, sizeof *p
);
24 struct gen_register_file
* rf
= GC_MALLOC(sizeof *rf
);
25 memset(rf
, 0, sizeof *rf
);
27 struct gen_stack
* stack
= GC_MALLOC(sizeof *stack
);
28 memset(rf
, 0, sizeof *stack
);
39 jvm_generate_function_header(gen_proc_t
* p
, char * access
, char * name
, char * signature
)
41 struct gen_class
* class;
44 /* Делаем процедуру текущей в этом классе */
45 assert(class -> p
== NULL
);
48 fprintf(class -> fp
, ".method %s %s%s\n", access
, name
, signature
);
49 fprintf(class -> fp
, " start:\n");
53 jvm_generate_function_end(gen_proc_t
* p
)
55 struct gen_class
* class;
61 int pointer
= p
-> stack
-> pointer
;
62 int max_pointer
= p
-> stack
-> max_pointer
;
63 int locals
= p
-> rf
-> num_used
;
64 int max_locals
= p
-> rf
-> max_used
;
66 fprintf(class -> fp
, " .limit stack %i \t; current(%i)\n", max_pointer
, pointer
);
67 fprintf(class -> fp
, " .limit locals %i \t; current(%i)\n", max_locals
, locals
);
68 fprintf(class -> fp
, " end:\n");
69 fprintf(class -> fp
, ".end method\n\n");
73 jvm_create_class(char * full_name
)
75 struct gen_class
* class = GC_MALLOC(sizeof *class);
76 memset(class, 0, sizeof *class);
78 char * fname
= new_string("%s.j", full_name
);
79 class -> full_name
= new_string(full_name
);
80 class -> fp
= fopen(fname
, "w");
87 jvm_destroy_class(struct gen_class
* class)
89 assert(class -> p
== NULL
);
94 jvm_stack_push(gen_proc_t
* p
, unsigned size
)
96 p
-> stack
-> pointer
+= size
;
97 if(p
-> stack
-> pointer
> p
-> stack
-> max_pointer
)
99 p
-> stack
-> max_pointer
= p
-> stack
-> pointer
;
104 jvm_stack_pop(gen_proc_t
* p
, unsigned size
)
106 p
-> stack
-> pointer
-= size
;
108 if(p
-> stack
-> pointer
< 0)
110 printf("WARING: stack pointer %i\n", p
-> stack
-> pointer
);
115 jvm_generate(gen_proc_t
* p
, unsigned get
, unsigned push
, char * format
, ...)
118 va_start(ptr
, format
);
120 jvm_stack_pop(p
, get
);
121 fprintf(p
-> class -> fp
, " ");
122 vfprintf(p
-> class -> fp
, format
, ptr
);
123 jvm_stack_push(p
, push
);
124 fprintf(p
-> class -> fp
, " \t ;>>>> %i -- %i : current_stack(%i)\n", get
, push
, p
-> stack
-> pointer
);
130 jvm_generate_comment(gen_proc_t
* p
, char * format
, ...)
133 va_start(ptr
, format
);
135 fprintf(p
-> class -> fp
, " ;;;; ");
136 vfprintf(p
-> class -> fp
, format
, ptr
);
137 fprintf(p
-> class -> fp
, "\n");
143 jvm_generate_push_int(gen_proc_t
* p
, int64_t i
)
147 jvm_generate(p
, 0, 1, "iconst_m1");
149 else if(i
>= 0 && i
<= 5)
151 jvm_generate(p
, 0, 1, "iconst_%li", i
);
153 else if(i
>= -128 && i
<= 127)
155 jvm_generate(p
, 0, 1, "bipush %li", i
);
157 else if(i
>= -32768 && i
<= 32767)
159 jvm_generate(p
, 0, 1, "sipush %li", i
);
161 else if(i
>= -2147483648 && i
<= 2147483647)
163 jvm_generate(p
, 0, 1, "ldc %li", i
);
167 jvm_generate(p
, 0, 2, "ldc2_w %li", i
);
172 jvm_generate_push_int_size(gen_proc_t
* p
, int64_t i
, int size
)
178 jvm_generate(p
, 0, 1, "iconst_m1");
180 else if(i
>= 0 && i
<= 5)
182 jvm_generate(p
, 0, 1, "iconst_%li", i
);
184 else if(i
>= -128 && i
<= 127)
186 jvm_generate(p
, 0, 1, "bipush %li", i
);
188 else if(i
>= -32768 && i
<= 32767)
190 jvm_generate(p
, 0, 1, "sipush %li", i
);
192 else if(i
>= -2147483648 && i
<= 2147483647)
194 jvm_generate(p
, 0, 1, "ldc %li", i
);
199 jvm_generate(p
, 0, 2, "ldc2_w %li", i
);
203 if(size
> 4 && pushed_cell
== 1)
205 jvm_generate(p
, pushed_cell
, 2, "i2l");
211 jvm_generate(p
, 2, 1, "l2i");
216 jvm_generate(p
, 1, 1, "i2s");
220 jvm_generate(p
, 1, 1, "i2b");
226 jvm_generate_push_float(gen_proc_t
* p
, double f
, int size
)
232 jvm_generate(p
, 0, 1, "fconst_0");
236 jvm_generate(p
, 0, 1, "fconst_1");
240 jvm_generate(p
, 0, 1, "fconst_2");
244 jvm_generate(p
, 0, 1, "ldc %lf", f
);
251 jvm_generate(p
, 0, 2, "dconst_0");
255 jvm_generate(p
, 0, 2, "dconst_1");
259 jvm_generate(p
, 0, 2, "ldc2_w %lf", f
);
265 jvm_generate_push_string(gen_proc_t
* p
, char * str
, int char_size
)
267 assert(char_size
== 1);
268 int len
= strlen(str
);
270 jvm_generate_push_int(p
, len
+ 1);
271 jvm_generate(p
, 1, 1, "newarray byte");
273 for(int i
= 0; i
< len
; i
++)
275 jvm_generate(p
, 1, 2, "dup");
276 jvm_generate_push_int(p
, i
);
277 jvm_generate_push_int(p
, str
[i
]);
278 jvm_generate(p
, 3, 0, "bastore");
283 jvm_new_label_id(gen_proc_t
* p
)
285 int label_id
= p
-> label_id
;
291 jvm_generate_label(gen_proc_t
* p
, int label_id
)
293 jvm_generate(p
, 0, 0, "L%i:", label_id
);