24c28b057429313018bf1529c64154c1e2a27825
10 #include "../../../include/oberon.h"
11 #include "../../oberon-internals.h"
12 #include "generator-jvm.h"
13 #include "generator-jvm-basic.h"
16 new_string(const char * format
, ...)
19 va_start(ptr
, format
);
22 vsnprintf(buf
, 1024, format
, ptr
);
30 result
= GC_MALLOC(size
+ 1);
31 memset(result
, 0, size
);
32 strncpy(result
, buf
, size
);
38 gen_error(const char * fmt
, ...)
42 fprintf(stderr
, "generator: ");
43 vfprintf(stderr
, fmt
, ptr
);
44 fprintf(stderr
, "\n");
50 jvm_get_descriptor(oberon_type_t
* type
)
56 case OBERON_TYPE_VOID
:
57 return new_string("V");
59 case OBERON_TYPE_INTEGER
:
63 return new_string("B");
66 return new_string("S");
69 return new_string("I");
72 return new_string("J");
75 gen_error("jvm_get_descriptor: unsupported int size %i", type
-> size
);
79 case OBERON_TYPE_REAL
:
83 return new_string("F");
86 return new_string("D");
89 gen_error("jvm_get_descriptor: unsupported float size %i", type
-> size
);
93 case OBERON_TYPE_CHAR
:
97 return new_string("B");
100 return new_string("C");
103 return new_string("I");
106 return new_string("J");
109 gen_error("jvm_get_descriptor: unsupported char size %i", type
-> size
);
113 case OBERON_TYPE_BOOLEAN
:
114 return new_string("Z");
116 case OBERON_TYPE_POINTER
:
117 return jvm_get_descriptor(type
-> base
);
119 case OBERON_TYPE_PROCEDURE
:
120 case OBERON_TYPE_RECORD
:
121 desc
= jvm_get_class_full_name(type
);
122 return new_string("L%s;", desc
);
124 case OBERON_TYPE_ARRAY
:
125 desc
= jvm_get_descriptor(type
-> base
);
126 return new_string("[%s", desc
);
129 gen_error("jvm_get_descriptor: unsupported type class %i", type
-> class);
137 jvm_get_descriptor_safe(oberon_type_t
* type
)
139 switch(type
-> class)
141 case OBERON_TYPE_POINTER
:
142 return new_string("PTR%s", jvm_get_descriptor_safe(type
-> base
));
144 case OBERON_TYPE_PROCEDURE
:
145 case OBERON_TYPE_RECORD
:
146 return jvm_get_class_full_name(type
);
148 case OBERON_TYPE_ARRAY
:
149 return new_string("A%sX", jvm_get_descriptor_safe(type
-> base
));
152 return jvm_get_descriptor(type
);
160 jvm_get_prefix(oberon_type_t
* type
)
162 int size
= type
-> size
;
163 switch(type
-> class)
165 case OBERON_TYPE_BOOLEAN
:
166 case OBERON_TYPE_INTEGER
:
167 case OBERON_TYPE_CHAR
:
168 return (size
<= 4) ? ('i') : ('l');
170 case OBERON_TYPE_PROCEDURE
:
171 case OBERON_TYPE_ARRAY
:
172 case OBERON_TYPE_RECORD
:
173 case OBERON_TYPE_POINTER
:
176 case OBERON_TYPE_REAL
:
177 return (size
<= 4) ? ('f') : ('d');
180 gen_error("jvm_get_prefix: wat");
187 jvm_get_postfix(oberon_type_t
* type
)
189 int size
= type
-> size
;
190 switch(type
-> class)
192 case OBERON_TYPE_BOOLEAN
:
195 case OBERON_TYPE_INTEGER
:
211 gen_error("jvm_get_postfix: int wat");
215 case OBERON_TYPE_CHAR
:
231 gen_error("jvm_get_postfix: char wat");
235 case OBERON_TYPE_PROCEDURE
:
236 case OBERON_TYPE_ARRAY
:
237 case OBERON_TYPE_RECORD
:
238 case OBERON_TYPE_POINTER
:
241 case OBERON_TYPE_REAL
:
242 return (size
<= 4) ? ('f') : ('d');
245 gen_error("jvm_get_postfix: wat");
253 jvm_get_field_full_name(oberon_object_t
* x
)
257 case OBERON_CLASS_VAR
:
258 case OBERON_CLASS_PROC
:
259 return new_string("%s/%s", x
-> module
-> name
, x
-> name
);
260 case OBERON_CLASS_FIELD
:;
261 char * rec_name
= jvm_get_class_full_name(x
-> parent_type
);
262 return new_string("%s/%s", rec_name
, x
-> name
);
263 case OBERON_CLASS_MODULE
:
264 return new_string(x
-> module
-> name
);
266 gen_error("jvm_get_field_full_name: wat");
274 jvm_get_field_full_name_safe(oberon_object_t
* x
)
278 case OBERON_CLASS_VAR
:
279 case OBERON_CLASS_PROC
:
280 return new_string("%s$%s", x
-> module
-> name
, x
-> name
);
281 case OBERON_CLASS_FIELD
:;
282 char * rec_name
= jvm_get_class_full_name(x
-> parent_type
);
283 return new_string("%s$%s", rec_name
, x
-> name
);
284 case OBERON_CLASS_MODULE
:
285 return new_string(x
-> module
-> name
);
287 gen_error("jvm_get_field_full_name: wat");
295 jvm_get_class_full_name(oberon_type_t
* type
)
300 switch(type
-> class)
302 case OBERON_TYPE_POINTER
:
303 name
= jvm_get_class_full_name(type
-> base
);
305 case OBERON_TYPE_PROCEDURE
:
306 name
= new_string("SYSTEM$PROCEDURE");
309 desc
= jvm_get_descriptor_safe(type
-> base
);
310 name
= new_string("%s$%s", name
, desc
);
312 int num
= type
-> num_decl
;
313 oberon_object_t
* arg
= type
-> decl
;
314 for(int i
= 0; i
< num
; i
++)
316 desc
= jvm_get_descriptor_safe(arg
-> type
);
317 name
= new_string("%s%s", name
, desc
);
322 case OBERON_TYPE_RECORD
:
323 rec_id
= type
-> gen_type
-> rec_id
;
324 name
= new_string("%s$RECORD%i", type
-> module
-> name
, rec_id
);
327 gen_error("jvm_get_class_full_name: unk type class %i", type
-> class);
335 jvm_get_procedure_signature(oberon_type_t
* proc
)
340 signature
= new_string("(");
342 int num
= proc
-> num_decl
;
343 oberon_object_t
* arg
= proc
-> decl
;
344 for(int i
= 0; i
< num
; i
++)
346 desc
= jvm_get_descriptor(arg
-> type
);
347 signature
= new_string("%s%s", signature
, desc
);
351 desc
= jvm_get_descriptor(proc
-> base
);
352 signature
= new_string("%s)%s", signature
, desc
);
358 jvm_cell_size_for_type(oberon_type_t
* type
)
360 if(type
-> class == OBERON_TYPE_INTEGER
361 || type
-> class == OBERON_TYPE_REAL
)
373 jvm_cell_size_for_postfix(char postfix
)
387 gen_error("jvm_cell_size_for_postfix: unk postfix %c", postfix
);
394 jvm_is_wide_type(oberon_type_t
* type
)
397 cell
= jvm_cell_size_for_type(type
);
403 jvm_is_free_register(struct gen_register_file
* rf
, int i
, bool wide
)
407 assert(i
+ 1 < MAX_REGISTERS
);
408 return !(rf
-> reg
[i
].used
|| rf
-> reg
[i
+ 1].used
);
412 assert(i
< MAX_REGISTERS
);
413 return !(rf
-> reg
[i
].used
);
418 jvm_alloc_register_untyped(struct gen_register_file
* rf
, bool wide
)
421 while(i
< MAX_REGISTERS
&& !jvm_is_free_register(rf
, i
, wide
))
428 assert(i
+ 1 <= MAX_REGISTERS
);
430 rf
-> reg
[i
].used
= true;
431 rf
-> reg
[i
+ 1].used
= true;
432 rf
-> reg
[i
].used
= true;
433 rf
-> reg
[i
+ 1].wide
= false;
437 assert(i
<= MAX_REGISTERS
);
439 rf
-> reg
[i
].used
= true;
440 rf
-> reg
[i
].wide
= false;
443 if(rf
-> num_used
> rf
-> max_used
)
445 rf
-> max_used
= rf
-> num_used
;
452 jvm_alloc_register(struct gen_register_file
* rf
, oberon_type_t
* type
)
455 wide
= jvm_is_wide_type(type
);
456 return jvm_alloc_register_untyped(rf
, wide
);
460 jvm_get_type_of_prefix(char prefix
)