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