* src/vm/signal.c (signal_thread): Restart sigwait if it has been
[cacao.git] / src / cacaoh / dummy.cpp
1 /* src/cacaoh/dummy.cpp - 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.hpp"
36 #include "mm/memory.h"
37
38 #include "native/llni.h"
39
40 #include "toolbox/logging.h"
41
42 #include "vm/exceptions.hpp"
43 #include "vm/global.h"
44 #include "vm/primitive.hpp"
45 #include "vm/vm.hpp"
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/os.hpp"
56
57
58 // Below this line are C exports.
59 extern "C" {
60
61 /* global variables ***********************************************************/
62
63 bool  vm_initializing = true;
64 char *_Jv_bootclasspath;
65
66
67 java_handle_t *javastring_new_slash_to_dot(utf *u)
68 {
69         vm_abort("javastring_new_slash_to_dot");
70
71         return NULL;
72 }
73
74
75 /* access *********************************************************************/
76
77 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
78 {
79         return true;
80 }
81
82 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
83                                                                  int32_t memberflags)
84 {
85         vm_abort("access_is_accessible_member");
86
87         return true;
88 }
89
90
91 /* array **********************************************************************/
92
93 java_handle_t *array_objectarray_element_get(java_handle_objectarray_t *a, int32_t index)
94 {
95         java_handle_t *value;
96         int32_t        size;
97
98         if (a == NULL) {
99                 log_println("array_objectarray_element_get(a=%p, index=%d): NullPointerException", a, index);
100                 return NULL;
101         }
102
103         size = LLNI_array_size(a);
104
105         if ((index < 0) || (index > size)) {
106                 log_println("array_objectarray_element_get(a=%p, index=%d): ArrayIndexOutOfBoundsException", a, index);
107                 return NULL;
108         }
109
110         value = LLNI_WRAP(LLNI_array_direct(a, index));
111
112         return value;
113 }
114
115 void array_objectarray_element_set(java_handle_objectarray_t *a, int32_t index, java_handle_t *value)
116 {
117         int32_t size;
118
119         if (a == NULL) {
120                 log_println("array_objectarray_element_set(a=%p, index=%d): NullPointerException", a, index);
121                 return;
122         }
123
124         size = LLNI_array_size(a);
125
126         if ((index < 0) || (index > size)) {
127                 log_println("array_objectarray_element_set(a=%p, index=%d): ArrayIndexOutOfBoundsException", a, index);
128                 return;
129         }
130
131         LLNI_array_direct(a, index) = LLNI_UNWRAP(value);
132 }
133
134 int32_t array_length_get(java_handle_t *a)
135 {
136         if (a == NULL) {
137                 log_println("array_length_get(a=%p): NullPointerException", a);
138                 return 0;
139         }
140
141         return LLNI_array_size(a);
142 }
143
144
145 /* asm ************************************************************************/
146
147 void asm_abstractmethoderror(void)
148 {
149         abort();
150 }
151
152 void intrp_asm_abstractmethoderror(void)
153 {
154         abort();
155 }
156
157
158 /* builtin ********************************************************************/
159
160 java_handle_t *builtin_clone(void *env, java_handle_t *o)
161 {
162         vm_abort("builtin_clone: Not implemented.");
163         return NULL;
164 }
165
166 bool builtin_isanysubclass(classinfo *sub, classinfo *super)
167 {
168         vm_abort("builtin_isanysubclass: Not implemented.");
169         return 0;
170 }
171
172 bool builtin_instanceof(java_handle_t *o, classinfo *c)
173 {
174         vm_abort("builtin_instanceof: Not implemented.");
175         return 0;
176 }
177
178 java_handle_t *builtin_new(classinfo *c)
179 {
180         vm_abort("builtin_new: Not implemented.");
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(void *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(size_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 /* instruction ****************************************************************/
470
471 methoddesc *instruction_call_site(const instruction *iptr)
472 {
473         return NULL;
474 }
475
476
477 /* jit ************************************************************************/
478
479 icmdtable_entry_t icmd_table[256] = {};
480
481 void jit_invalidate_code(methodinfo *m)
482 {
483         vm_abort("jit_invalidate_code");
484 }
485
486
487 /* llni ***********************************************************************/
488
489 void llni_critical_start()
490 {
491 }
492
493 void llni_critical_end()
494 {
495 }
496
497
498 /* localref *******************************************************************/
499
500 java_handle_t *localref_add(java_object_t *o)
501 {
502 #if defined(ENABLE_HANDLES)
503         java_handle_t *h = (java_handle_t*) mem_alloc(sizeof(java_handle_t));
504
505         h->heap_object = o;
506
507         return h;
508 #else
509         return (java_handle_t*) o;
510 #endif
511 }
512
513
514 /* lock ***********************************************************************/
515
516 void lock_init_object_lock(java_object_t *o)
517 {
518 }
519
520 bool lock_monitor_enter(java_handle_t *o)
521 {
522         return true;
523 }
524
525 bool lock_monitor_exit(java_handle_t *o)
526 {
527         return true;
528 }
529
530
531 /* md *************************************************************************/
532
533 void md_param_alloc(methoddesc *md)
534 {
535 }
536
537 void md_param_alloc_native(methoddesc *md)
538 {
539 }
540
541
542 /* memory *********************************************************************/
543
544 void *mem_alloc(int32_t size)
545 {
546         /* real implementation in src/mm/memory.c clears memory */
547
548         return calloc(size, 1);
549 }
550
551 void *mem_realloc(void *src, int32_t len1, int32_t len2)
552 {
553         return realloc(src, len2);
554 }
555
556 void mem_free(void *m, int32_t size)
557 {
558         free(m);
559 }
560
561 void *dumpmemory_get(size_t size)
562 {
563         return malloc(size);
564 }
565
566 int32_t dumpmemory_marker(void)
567 {
568         return 0;
569 }
570
571 void dumpmemory_release(int32_t size)
572 {
573 }
574
575
576 /* package ********************************************************************/
577
578 /* void Package_add(java_handle_t *packagename) */
579 void Package_add(utf *packagename)
580 {
581         /* Do nothing. */
582 }
583
584
585 /* primitive ******************************************************************/
586
587 classinfo *Primitive_get_arrayclass_by_type(int type)
588 {
589         return NULL;
590 }
591
592 classinfo *Primitive_get_class_by_type(int type)
593 {
594         abort();
595         return NULL;
596 }
597
598 classinfo *Primitive_get_class_by_char(char ch)
599 {
600         abort();
601         return NULL;
602 }
603
604
605 /* properties *****************************************************************/
606
607 void properties_add(char *key, char *value)
608 {
609 }
610
611 char *properties_get(char *key)
612 {
613         return NULL;
614 }
615
616
617 /* reflect ********************************************************************/
618
619 java_handle_t *reflect_constructor_new(fieldinfo *f)
620 {
621         vm_abort("reflect_constructor_new: Not implemented.");
622         return NULL;
623 }
624
625 java_handle_t *reflect_field_new(fieldinfo *f)
626 {
627         vm_abort("reflect_field_new: Not implemented.");
628         return NULL;
629 }
630
631 java_handle_t *reflect_method_new(methodinfo *m)
632 {
633         vm_abort("reflect_method_new: Not implemented.");
634         return NULL;
635 }
636
637
638 /* resolve ********************************************************************/
639
640 void resolve_handle_pending_exception(bool throwError)
641 {
642         vm_abort("resolve_handle_pending_exception: Not implemented.");
643 }
644
645 bool resolve_class_from_typedesc(typedesc *d, bool checkaccess, bool link, classinfo **result)
646 {
647         abort();
648
649         return false;
650 }
651
652 /* stupid resolving implementation used by resolve_classref_or_classinfo_eager */
653 /* This function does eager resolving without any access checks.               */
654
655 static classinfo * dummy_resolve_class_from_name(classinfo *referer,
656                                                  utf *classname,
657                                                  bool checkaccess)
658 {
659         classinfo *cls = NULL;
660         char *utf_ptr;
661         int len;
662         
663         assert(referer);
664         assert(classname);
665         
666         /* lookup if this class has already been loaded */
667
668         cls = classcache_lookup(referer->classloader, classname);
669
670         if (!cls) {
671                 /* resolve array types */
672
673                 if (classname->text[0] == '[') {
674                         utf_ptr = classname->text + 1;
675                         len = classname->blength - 1;
676
677                         /* classname is an array type name */
678
679                         switch (*utf_ptr) {
680                                 case 'L':
681                                         utf_ptr++;
682                                         len -= 2;
683                                         /* FALLTHROUGH */
684                                 case '[':
685                                         /* the component type is a reference type */
686                                         /* resolve the component type */
687                                         if ((cls = dummy_resolve_class_from_name(referer,
688                                                                            utf_new(utf_ptr,len),
689                                                                            checkaccess)) == NULL)
690                                                 return NULL; /* exception */
691
692                                         /* create the array class */
693                                         cls = class_array_of(cls,false);
694                                         if (!cls)
695                                                 return NULL; /* exception */
696                         }
697                 }
698
699                 /* load the class */
700                 if (!cls) {
701                         if (!(cls = load_class_from_classloader(classname,
702                                                                                                         referer->classloader)))
703                                 return false; /* exception */
704                 }
705         }
706
707         /* the class is now loaded */
708         assert(cls);
709         assert(cls->state & CLASS_LOADED);
710
711         return cls;
712 }
713
714
715 classinfo * resolve_classref_or_classinfo_eager(classref_or_classinfo cls,
716                                                                                                 bool checkaccess)
717 {
718         classinfo         *c;
719         
720         assert(cls.any);
721
722         if (IS_CLASSREF(cls)) {
723                 /* we must resolve this reference */
724
725                 if ((c = dummy_resolve_class_from_name(cls.ref->referer, cls.ref->name,
726                                                                                    checkaccess)) == NULL)
727                         return NULL;
728         }
729         else {
730                 /* cls has already been resolved */
731                 c = cls.cls;
732         }
733
734         assert(c);
735         assert(c->state & CLASS_LOADED);
736
737         /* succeeded */
738         return c;
739 }
740
741
742 /* stacktrace *****************************************************************/
743
744 java_handle_objectarray_t *stacktrace_getClassContext()
745 {
746         return NULL;
747 }
748
749
750 /* threads ********************************************************************/
751
752 #if defined(HAVE___THREAD)
753 __thread threadobject *thread_current;
754 #else
755 #include <pthread.h>
756 pthread_key_t thread_current_key;
757 #endif
758
759 intptr_t threads_get_current_tid(void)
760 {
761         return 0;
762 }
763
764 void threads_cast_stopworld(void)
765 {
766 }
767
768 void threads_cast_startworld(void)
769 {
770 }
771
772
773 /* vm *************************************************************************/
774
775 void vm_printconfig(void)
776 {
777 }
778
779 void vm_abort(const char *text, ...)
780 {
781         va_list ap;
782
783         va_start(ap, text);
784         vfprintf(stderr, text, ap);
785         va_end(ap);
786
787         os::abort();
788 }
789
790 void vm_abort_errno(const char *text, ...)
791 {
792         va_list ap;
793
794         va_start(ap, text);
795         vm_abort_errnum(errno, text, ap);
796         va_end(ap);
797 }
798
799 void vm_abort_errnum(int errnum, const char *text, ...)
800 {
801         va_list ap;
802
803         log_start();
804
805         va_start(ap, text);
806         log_vprint(text, ap);
807         va_end(ap);
808
809         log_print(": %s", os::strerror(errnum));
810         log_finish();
811
812         os::abort();
813 }
814
815 java_handle_t *vm_call_method(methodinfo *m, java_handle_t *o, ...)
816 {
817         return NULL;
818 }
819
820
821 /* XXX */
822
823 void stringtable_update(void)
824 {
825         log_println("stringtable_update: REMOVE ME!");
826 }
827
828 java_object_t *literalstring_new(utf *u)
829 {
830         log_println("literalstring_new: REMOVE ME!");
831
832         return NULL;
833 }
834
835
836 void print_dynamic_super_statistics(void)
837 {
838 }
839
840
841 #if defined(ENABLE_VMLOG)
842 void vmlog_cacao_set_prefix(const char *arg)
843 {
844 }
845
846 void vmlog_cacao_set_stringprefix(const char *arg)
847 {
848 }
849
850 void vmlog_cacao_set_ignoreprefix(const char *arg)
851 {
852 }
853 #endif
854
855
856 /* Legacy C interface *********************************************************/
857
858 bool VM_is_initializing() { return true; }
859
860 } // extern "C"
861
862
863 /*
864  * These are local overrides for various environment variables in Emacs.
865  * Please do not remove this and leave it at the end of the file, where
866  * Emacs will automagically detect them.
867  * ---------------------------------------------------------------------
868  * Local variables:
869  * mode: c++
870  * indent-tabs-mode: t
871  * c-basic-offset: 4
872  * tab-width: 4
873  * End:
874  * vim:noexpandtab:sw=4:ts=4:
875  */