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