Merge -> trunk
[cacao.git] / src / cacaoh / dummy.c
1 /* src/cacaoh/dummy.c - dummy functions for cacaoh
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "mm/memory.h"
36
37 #include "native/llni.h"
38
39 #include "toolbox/logging.h"
40
41 #include "vm/exceptions.h"
42 #include "vm/global.h"
43 #include "vm/primitive.h"
44 #include "vm/vm.h"
45
46 #include "vmcore/class.h"
47 #include "vmcore/classcache.h"
48 #include "vmcore/loader.h"
49 #include "vmcore/method.h"
50 #include "vmcore/utf8.h"
51 #include "vmcore/system.h"
52
53
54 /* global variables ***********************************************************/
55
56 bool  vm_initializing = true;
57 char *_Jv_bootclasspath;
58
59
60 java_handle_t *javastring_new_slash_to_dot(utf *u)
61 {
62         vm_abort("javastring_new_slash_to_dot");
63
64         return NULL;
65 }
66
67
68 /* access *********************************************************************/
69
70 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
71 {
72         return true;
73 }
74
75 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
76                                                                  int32_t memberflags)
77 {
78         vm_abort("access_is_accessible_member");
79
80         return true;
81 }
82
83
84 /* array **********************************************************************/
85
86 java_handle_t *array_objectarray_element_get(java_handle_objectarray_t *a, int32_t index)
87 {
88         java_handle_t *value;
89         int32_t        size;
90
91         if (a == NULL) {
92                 log_println("array_objectarray_element_get(a=%p, index=%d): NullPointerException", a, index);
93                 return NULL;
94         }
95
96         size = LLNI_array_size(a);
97
98         if ((index < 0) || (index > size)) {
99                 log_println("array_objectarray_element_get(a=%p, index=%d): ArrayIndexOutOfBoundsException", a, index);
100                 return NULL;
101         }
102
103         value = LLNI_WRAP(LLNI_array_direct(a, index));
104
105         return value;
106 }
107
108 void array_objectarray_element_set(java_handle_objectarray_t *a, int32_t index, java_handle_t *value)
109 {
110         int32_t size;
111
112         if (a == NULL) {
113                 log_println("array_objectarray_element_set(a=%p, index=%d): NullPointerException", a, index);
114                 return;
115         }
116
117         size = LLNI_array_size(a);
118
119         if ((index < 0) || (index > size)) {
120                 log_println("array_objectarray_element_set(a=%p, index=%d): ArrayIndexOutOfBoundsException", a, index);
121                 return;
122         }
123
124         LLNI_array_direct(a, index) = LLNI_UNWRAP(value);
125 }
126
127 int32_t array_length_get(java_handle_t *a)
128 {
129         if (a == NULL) {
130                 log_println("array_length_get(a=%p): NullPointerException", a);
131                 return 0;
132         }
133
134         return LLNI_array_size(a);
135 }
136
137
138 /* asm ************************************************************************/
139
140 void asm_abstractmethoderror(void)
141 {
142         abort();
143 }
144
145 void intrp_asm_abstractmethoderror(void)
146 {
147         abort();
148 }
149
150 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out)
151 {
152         abort();
153 }
154
155 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out)
156 {
157         abort();
158 }
159
160
161 /* builtin ********************************************************************/
162
163 java_handle_t *builtin_clone(void *env, java_handle_t *o)
164 {
165         abort();
166
167         return NULL;
168 }
169
170 int32_t builtin_isanysubclass(classinfo *sub, classinfo *super)
171 {
172         abort();
173
174         return 0;
175 }
176
177 java_handle_t *builtin_new(classinfo *c)
178 {
179         abort();
180
181         return NULL;
182 }
183
184 java_handle_objectarray_t *builtin_anewarray(int32_t size, classinfo *componentclass)
185 {
186         java_objectarray_t *oa = (java_objectarray_t*) mem_alloc(
187                 sizeof(java_array_t) + size * sizeof(java_object_t*));
188         java_handle_objectarray_t *h = (java_handle_objectarray_t*) LLNI_WRAP(
189                 (java_object_t*) oa);
190
191         if (h != NULL) {
192                 LLNI_array_size(h) = size;
193         }
194
195         return h;
196 }
197
198 java_handle_bytearray_t *builtin_newarray_byte(int32_t size)
199 {
200         java_bytearray_t *ba = (java_bytearray_t*) mem_alloc(
201                 sizeof(java_array_t) + size * sizeof(int8_t));
202         java_handle_bytearray_t *h = (java_handle_bytearray_t*) LLNI_WRAP(
203                 (java_object_t*) ba);
204
205         if (h != NULL) {
206                 LLNI_array_size(h) = size;
207         }
208         
209         return h;
210 }
211
212
213 /* code ***********************************************************************/
214
215 void code_free_code_of_method(methodinfo *m)
216 {
217 }
218
219
220 methodinfo *code_get_methodinfo_for_pv(u1 *pv)
221 {
222         return NULL;
223 }
224
225
226 /* codegen ********************************************************************/
227
228 u1 *codegen_generate_stub_compiler(methodinfo *m)
229 {
230         return NULL;
231 }
232
233 codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f)
234 {
235         return NULL;
236 }
237
238 #if defined(ENABLE_INTRP)
239 u1 *intrp_createcompilerstub(methodinfo *m)
240 {
241         return NULL;
242 }
243 #endif
244
245 void removecompilerstub(u1 *stub)
246 {
247 }
248
249 void removenativestub(u1 *stub)
250 {
251 }
252
253
254 /* exceptions *****************************************************************/
255
256 void exceptions_clear_exception(void)
257 {
258 }
259
260 void exceptions_print_current_exception(void)
261 {
262         abort();
263 }
264
265 void exceptions_throw_abstractmethoderror(void)
266 {
267         fprintf(stderr, "java.lang.AbstractMethodError\n");
268
269         abort();
270 }
271
272 void exceptions_throw_classcircularityerror(classinfo *c)
273 {
274         fprintf(stderr, "java.lang.ClassCircularityError: ");
275
276         utf_display_printable_ascii(c->name);
277         fputc('\n', stderr);
278
279         abort();
280 }
281
282 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
283 {
284         va_list ap;
285
286         fprintf(stderr, "java.lang.ClassFormatError: ");
287
288         utf_display_printable_ascii(c->name);
289         fprintf(stderr, ": ");
290
291         va_start(ap, message);
292         vfprintf(stderr, message, ap);
293         va_end(ap);
294
295         fputc('\n', stderr);
296
297         abort();
298 }
299
300 void exceptions_throw_incompatibleclasschangeerror(classinfo *c, const char *message)
301 {
302         fprintf(stderr, "java.lang.IncompatibleClassChangeError: ");
303
304         if (c != NULL)
305                 utf_fprint_printable_ascii_classname(stderr, c->name);
306
307         fputc('\n', stderr);
308
309         abort();
310 }
311
312 void exceptions_throw_internalerror(const char *message, ...)
313 {
314         va_list ap;
315
316         fprintf(stderr, "java.lang.InternalError: ");
317
318         va_start(ap, message);
319         vfprintf(stderr, message, ap);
320         va_end(ap);
321
322         abort();
323 }
324
325 void exceptions_throw_linkageerror(const char *message, classinfo *c)
326 {
327         fprintf(stderr, "java.lang.LinkageError: %s", message);
328
329         if (c != NULL)
330                 utf_fprint_printable_ascii_classname(stderr, c->name);
331
332         fputc('\n', stderr);
333
334         abort();
335 }
336
337 void exceptions_throw_noclassdeffounderror(utf *name)
338 {
339         fprintf(stderr, "java.lang.NoClassDefFoundError: ");
340         utf_fprint_printable_ascii(stderr, name);
341         fputc('\n', stderr);
342
343         abort();
344 }
345
346 void exceptions_throw_noclassdeffounderror_wrong_name(classinfo *c, utf *name)
347 {
348         fprintf(stderr, "java.lang.NoClassDefFoundError: ");
349         utf_fprint_printable_ascii(stderr, c->name);
350         fprintf(stderr, " (wrong name: ");
351         utf_fprint_printable_ascii(stderr, name);
352         fprintf(stderr, ")\n");
353
354         abort();
355 }
356
357 void exceptions_throw_verifyerror(methodinfo *m, const char *message, ...)
358 {
359         fprintf(stderr, "java.lang.VerifyError: ");
360         utf_fprint_printable_ascii(stderr, m->name);
361         fprintf(stderr, ": %s", message);
362
363         abort();
364 }
365
366 void exceptions_throw_nosuchfielderror(classinfo *c, utf *name)
367 {
368         fprintf(stderr, "java.lang.NoSuchFieldError: ");
369         utf_fprint_printable_ascii(stderr, c->name);
370         fprintf(stderr, ".");
371         utf_fprint_printable_ascii(stderr, name);
372         fputc('\n', stderr);
373
374         abort();
375 }
376
377 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
378 {
379         fprintf(stderr, "java.lang.NoSuchMethodError: ");
380         utf_fprint_printable_ascii(stderr, c->name);
381         fprintf(stderr, ".");
382         utf_fprint_printable_ascii(stderr, name);
383         utf_fprint_printable_ascii(stderr, desc);
384         fputc('\n', stderr);
385
386         abort();
387 }
388
389 void exceptions_throw_unsupportedclassversionerror(classinfo *c, u4 ma, u4 mi)
390 {
391         fprintf(stderr, "java.lang.UnsupportedClassVersionError: " );
392         utf_display_printable_ascii(c->name);
393         fprintf(stderr, " (Unsupported major.minor version %d.%d)\n", ma, mi);
394
395         abort();
396 }
397
398 void exceptions_throw_classnotfoundexception(utf *name)
399 {
400         fprintf(stderr, "java.lang.ClassNotFoundException: ");
401         utf_fprint_printable_ascii(stderr, name);
402         fputc('\n', stderr);
403
404         abort();
405 }
406
407 void exceptions_throw_nullpointerexception(void)
408 {
409         fprintf(stderr, "java.lang.NullPointerException\n");
410
411         abort();
412 }
413
414
415 /* finalizer ******************************************************************/
416
417 void finalizer_notify(void)
418 {
419         vm_abort("finalizer_notify");
420 }
421
422 void finalizer_run(void *o, void *p)
423 {
424         vm_abort("finalizer_run");
425 }
426
427
428 /* gc *************************************************************************/
429
430 void gc_reference_register(java_object_t **ref, int32_t reftype)
431 {
432         vm_abort("gc_reference_register");
433 }
434
435 int64_t gc_get_heap_size(void)
436 {
437         return 0;
438 }
439
440 int64_t gc_get_free_bytes(void)
441 {
442         return 0;
443 }
444
445 int64_t gc_get_total_bytes(void)
446 {
447         return 0;
448 }
449
450 int64_t gc_get_max_heap_size(void)
451 {
452         return 0;
453 }
454
455
456 /* heap ***********************************************************************/
457
458 void *heap_alloc_uncollectable(uint32_t bytelength)
459 {
460         return calloc(bytelength, 1);
461 }
462
463 s4 heap_get_hashcode(java_object_t *o)
464 {
465         return 0;
466 }
467
468
469 /* jit ************************************************************************/
470
471 void jit_invalidate_code(methodinfo *m)
472 {
473         vm_abort("jit_invalidate_code");
474 }
475
476
477 /* llni ***********************************************************************/
478
479 void llni_critical_start()
480 {
481 }
482
483 void llni_critical_end()
484 {
485 }
486
487
488 /* localref *******************************************************************/
489
490 java_handle_t *localref_add(java_object_t *o)
491 {
492 #if defined(ENABLE_HANDLES)
493         java_handle_t *h = (java_handle_t*) mem_alloc(sizeof(java_handle_t));
494
495         h->heap_object = o;
496
497         return h;
498 #else
499         return (java_handle_t*) o;
500 #endif
501 }
502
503
504 /* lock ***********************************************************************/
505
506 void lock_init_object_lock(java_object_t *o)
507 {
508 }
509
510 bool lock_monitor_enter(java_handle_t *o)
511 {
512         return true;
513 }
514
515 bool lock_monitor_exit(java_handle_t *o)
516 {
517         return true;
518 }
519
520
521 /* md *************************************************************************/
522
523 void md_param_alloc(methoddesc *md)
524 {
525 }
526
527 void md_param_alloc_native(methoddesc *md)
528 {
529 }
530
531
532 /* memory *********************************************************************/
533
534 void *mem_alloc(int32_t size)
535 {
536         /* real implementation in src/mm/memory.c clears memory */
537
538         return calloc(size, 1);
539 }
540
541 void *mem_realloc(void *src, int32_t len1, int32_t len2)
542 {
543         return realloc(src, len2);
544 }
545
546 void mem_free(void *m, int32_t size)
547 {
548         free(m);
549 }
550
551 void *dumpmemory_get(size_t size)
552 {
553         return malloc(size);
554 }
555
556 int32_t dumpmemory_marker(void)
557 {
558         return 0;
559 }
560
561 void dumpmemory_release(int32_t size)
562 {
563 }
564
565
566 /* package ********************************************************************/
567
568 /* void package_add(java_handle_t *packagename) */
569 void package_add(utf *packagename)
570 {
571         /* Do nothing. */
572 }
573
574
575 /* primitive ******************************************************************/
576
577 classinfo *primitive_arrayclass_get_by_type(int type)
578 {
579         return NULL;
580 }
581
582 classinfo *primitive_class_get_by_type(int type)
583 {
584         abort();
585         return NULL;
586 }
587
588 classinfo *primitive_class_get_by_char(char ch)
589 {
590         abort();
591         return NULL;
592 }
593
594
595 /* properties *****************************************************************/
596
597 void properties_add(char *key, char *value)
598 {
599 }
600
601 char *properties_get(char *key)
602 {
603         return NULL;
604 }
605
606
607 /* resolve ********************************************************************/
608
609 bool resolve_class_from_typedesc(typedesc *d, bool checkaccess, bool link, classinfo **result)
610 {
611         abort();
612
613         return false;
614 }
615
616 /* stupid resolving implementation used by resolve_classref_or_classinfo_eager */
617 /* This function does eager resolving without any access checks.               */
618
619 static classinfo * dummy_resolve_class_from_name(classinfo *referer,
620                                                  utf *classname,
621                                                  bool checkaccess)
622 {
623         classinfo *cls = NULL;
624         char *utf_ptr;
625         int len;
626         
627         assert(referer);
628         assert(classname);
629         
630         /* lookup if this class has already been loaded */
631
632         cls = classcache_lookup(referer->classloader, classname);
633
634         if (!cls) {
635                 /* resolve array types */
636
637                 if (classname->text[0] == '[') {
638                         utf_ptr = classname->text + 1;
639                         len = classname->blength - 1;
640
641                         /* classname is an array type name */
642
643                         switch (*utf_ptr) {
644                                 case 'L':
645                                         utf_ptr++;
646                                         len -= 2;
647                                         /* FALLTHROUGH */
648                                 case '[':
649                                         /* the component type is a reference type */
650                                         /* resolve the component type */
651                                         if ((cls = dummy_resolve_class_from_name(referer,
652                                                                            utf_new(utf_ptr,len),
653                                                                            checkaccess)) == NULL)
654                                                 return NULL; /* exception */
655
656                                         /* create the array class */
657                                         cls = class_array_of(cls,false);
658                                         if (!cls)
659                                                 return NULL; /* exception */
660                         }
661                 }
662
663                 /* load the class */
664                 if (!cls) {
665                         if (!(cls = load_class_from_classloader(classname,
666                                                                                                         referer->classloader)))
667                                 return false; /* exception */
668                 }
669         }
670
671         /* the class is now loaded */
672         assert(cls);
673         assert(cls->state & CLASS_LOADED);
674
675         return cls;
676 }
677
678
679 classinfo * resolve_classref_or_classinfo_eager(classref_or_classinfo cls,
680                                                                                                 bool checkaccess)
681 {
682         classinfo         *c;
683         
684         assert(cls.any);
685
686         if (IS_CLASSREF(cls)) {
687                 /* we must resolve this reference */
688
689                 if ((c = dummy_resolve_class_from_name(cls.ref->referer, cls.ref->name,
690                                                                                    checkaccess)) == NULL)
691                         return NULL;
692         }
693         else {
694                 /* cls has already been resolved */
695                 c = cls.cls;
696         }
697
698         assert(c);
699         assert(c->state & CLASS_LOADED);
700
701         /* succeeded */
702         return c;
703 }
704
705
706 /* stacktrace *****************************************************************/
707
708 java_handle_objectarray_t *stacktrace_getClassContext()
709 {
710         return NULL;
711 }
712
713
714 /* threads ********************************************************************/
715
716 intptr_t threads_get_current_tid(void)
717 {
718         return 0;
719 }
720
721 void threads_cast_stopworld(void)
722 {
723 }
724
725 void threads_cast_startworld(void)
726 {
727 }
728
729
730 /* vm *************************************************************************/
731
732 void vm_printconfig(void)
733 {
734 }
735
736 void vm_abort(const char *text, ...)
737 {
738         va_list ap;
739
740         va_start(ap, text);
741         vfprintf(stderr, text, ap);
742         va_end(ap);
743
744         system_abort();
745 }
746
747 void vm_abort_errno(const char *text, ...)
748 {
749         va_list ap;
750
751         va_start(ap, text);
752         vm_abort_errnum(errno, text, ap);
753         va_end(ap);
754 }
755
756 void vm_abort_errnum(int errnum, const char *text, ...)
757 {
758         va_list ap;
759
760         log_start();
761
762         va_start(ap, text);
763         log_vprint(text, ap);
764         va_end(ap);
765
766         log_print(": %s", system_strerror(errnum));
767         log_finish();
768
769         system_abort();
770 }
771
772 java_handle_t *vm_call_method(methodinfo *m, java_handle_t *o, ...)
773 {
774         return NULL;
775 }
776
777
778 /* XXX */
779
780 void stringtable_update(void)
781 {
782         log_println("stringtable_update: REMOVE ME!");
783 }
784
785 java_object_t *literalstring_new(utf *u)
786 {
787         log_println("literalstring_new: REMOVE ME!");
788
789         return NULL;
790 }
791
792
793 void print_dynamic_super_statistics(void)
794 {
795 }
796
797
798 #if defined(ENABLE_VMLOG)
799 void vmlog_cacao_set_prefix(const char *arg)
800 {
801 }
802
803 void vmlog_cacao_set_stringprefix(const char *arg)
804 {
805 }
806
807 void vmlog_cacao_set_ignoreprefix(const char *arg)
808 {
809 }
810 #endif
811
812
813 /*
814  * These are local overrides for various environment variables in Emacs.
815  * Please do not remove this and leave it at the end of the file, where
816  * Emacs will automagically detect them.
817  * ---------------------------------------------------------------------
818  * Local variables:
819  * mode: c
820  * indent-tabs-mode: t
821  * c-basic-offset: 4
822  * tab-width: 4
823  * End:
824  * vim:noexpandtab:sw=4:ts=4:
825  */