* src/cacaoh/dummy.c (package_add): New function.
[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 char *_Jv_bootclasspath;
56
57
58 java_handle_t *javastring_new_slash_to_dot(utf *u)
59 {
60         vm_abort("javastring_new_slash_to_dot");
61
62         return NULL;
63 }
64
65
66 /* access *********************************************************************/
67
68 bool access_is_accessible_class(classinfo *referer, classinfo *cls)
69 {
70         return true;
71 }
72
73 bool access_is_accessible_member(classinfo *referer, classinfo *declarer,
74                                                                  int32_t memberflags)
75 {
76         vm_abort("access_is_accessible_member");
77
78         return true;
79 }
80
81
82 /* array **********************************************************************/
83
84 java_handle_t *array_objectarray_element_get(java_handle_objectarray_t *a, int32_t index)
85 {
86         java_handle_t *value;
87         int32_t        size;
88
89         if (a == NULL) {
90                 log_println("array_objectarray_element_get(a=%p, index=%d): NullPointerException", a, index);
91                 return NULL;
92         }
93
94         size = LLNI_array_size(a);
95
96         if ((index < 0) || (index > size)) {
97                 log_println("array_objectarray_element_get(a=%p, index=%d): ArrayIndexOutOfBoundsException", a, index);
98                 return NULL;
99         }
100
101         LLNI_objectarray_element_get(a, index, value);
102
103         return value;
104 }
105
106 void array_objectarray_element_set(java_handle_objectarray_t *a, int32_t index, java_handle_t *value)
107 {
108         int32_t size;
109
110         if (a == NULL) {
111                 log_println("array_objectarray_element_set(a=%p, index=%d): NullPointerException", a, index);
112                 return;
113         }
114
115         size = LLNI_array_size(a);
116
117         if ((index < 0) || (index > size)) {
118                 log_println("array_objectarray_element_set(a=%p, index=%d): ArrayIndexOutOfBoundsException", a, index);
119                 return;
120         }
121
122         LLNI_objectarray_element_set(a, index, value);
123 }
124
125 int32_t array_length_get(java_handle_t *a)
126 {
127         if (a == NULL) {
128                 log_println("array_length_get(a=%p): NullPointerException", a);
129                 return 0;
130         }
131
132         return LLNI_array_size(a);
133 }
134
135
136 /* asm ************************************************************************/
137
138 void asm_abstractmethoderror(void)
139 {
140         abort();
141 }
142
143 void intrp_asm_abstractmethoderror(void)
144 {
145         abort();
146 }
147
148 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out)
149 {
150         abort();
151 }
152
153 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out)
154 {
155         abort();
156 }
157
158
159 /* builtin ********************************************************************/
160
161 java_handle_t *builtin_clone(void *env, java_handle_t *o)
162 {
163         abort();
164
165         return NULL;
166 }
167
168 int32_t builtin_isanysubclass(classinfo *sub, classinfo *super)
169 {
170         abort();
171
172         return 0;
173 }
174
175 java_handle_t *builtin_new(classinfo *c)
176 {
177         abort();
178
179         return NULL;
180 }
181
182 java_handle_objectarray_t *builtin_anewarray(int32_t size, classinfo *componentclass)
183 {
184         java_objectarray_t *oa = (java_objectarray_t*) mem_alloc(
185                 sizeof(java_array_t) + size * sizeof(java_object_t*));
186         java_handle_objectarray_t *h = (java_handle_objectarray_t*) LLNI_WRAP(
187                 (java_object_t*) oa);
188
189         if (h != NULL) {
190                 LLNI_array_size(h) = size;
191         }
192
193         return h;
194 }
195
196 java_handle_bytearray_t *builtin_newarray_byte(int32_t size)
197 {
198         java_bytearray_t *ba = (java_bytearray_t*) mem_alloc(
199                 sizeof(java_array_t) + size * sizeof(int8_t));
200         java_handle_bytearray_t *h = (java_handle_bytearray_t*) LLNI_WRAP(
201                 (java_object_t*) ba);
202
203         if (h != NULL) {
204                 LLNI_array_size(h) = size;
205         }
206         
207         return h;
208 }
209
210
211 /* code ***********************************************************************/
212
213 void code_free_code_of_method(methodinfo *m)
214 {
215 }
216
217
218 methodinfo *code_get_methodinfo_for_pv(u1 *pv)
219 {
220         return NULL;
221 }
222
223
224 /* codegen ********************************************************************/
225
226 u1 *codegen_generate_stub_compiler(methodinfo *m)
227 {
228         return NULL;
229 }
230
231 codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f)
232 {
233         return NULL;
234 }
235
236 #if defined(ENABLE_INTRP)
237 u1 *intrp_createcompilerstub(methodinfo *m)
238 {
239         return NULL;
240 }
241 #endif
242
243 void removecompilerstub(u1 *stub)
244 {
245 }
246
247 void removenativestub(u1 *stub)
248 {
249 }
250
251
252 /* exceptions *****************************************************************/
253
254 void exceptions_clear_exception(void)
255 {
256 }
257
258 void exceptions_print_current_exception(void)
259 {
260         abort();
261 }
262
263 void exceptions_throw_abstractmethoderror(void)
264 {
265         fprintf(stderr, "java.lang.AbstractMethodError\n");
266
267         abort();
268 }
269
270 void exceptions_throw_classcircularityerror(classinfo *c)
271 {
272         fprintf(stderr, "java.lang.ClassCircularityError: ");
273
274         utf_display_printable_ascii(c->name);
275         fputc('\n', stderr);
276
277         abort();
278 }
279
280 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
281 {
282         va_list ap;
283
284         fprintf(stderr, "java.lang.ClassFormatError: ");
285
286         utf_display_printable_ascii(c->name);
287         fprintf(stderr, ": ");
288
289         va_start(ap, message);
290         vfprintf(stderr, message, ap);
291         va_end(ap);
292
293         fputc('\n', stderr);
294
295         abort();
296 }
297
298 void exceptions_throw_incompatibleclasschangeerror(classinfo *c)
299 {
300         fprintf(stderr, "java.lang.IncompatibleClassChangeError: ");
301
302         if (c != NULL)
303                 utf_fprint_printable_ascii_classname(stderr, c->name);
304
305         fputc('\n', stderr);
306
307         abort();
308 }
309
310 void exceptions_throw_internalerror(const char *message, ...)
311 {
312         va_list ap;
313
314         fprintf(stderr, "java.lang.InternalError: ");
315
316         va_start(ap, message);
317         vfprintf(stderr, message, ap);
318         va_end(ap);
319
320         abort();
321 }
322
323 void exceptions_throw_linkageerror(const char *message, classinfo *c)
324 {
325         fprintf(stderr, "java.lang.LinkageError: %s", message);
326
327         if (c != NULL)
328                 utf_fprint_printable_ascii_classname(stderr, c->name);
329
330         fputc('\n', stderr);
331
332         abort();
333 }
334
335 void exceptions_throw_noclassdeffounderror(utf *name)
336 {
337         fprintf(stderr, "java.lang.NoClassDefFoundError: ");
338         utf_fprint_printable_ascii(stderr, name);
339         fputc('\n', stderr);
340
341         abort();
342 }
343
344 void exceptions_throw_noclassdeffounderror_wrong_name(classinfo *c, utf *name)
345 {
346         fprintf(stderr, "java.lang.NoClassDefFoundError: ");
347         utf_fprint_printable_ascii(stderr, c->name);
348         fprintf(stderr, " (wrong name: ");
349         utf_fprint_printable_ascii(stderr, name);
350         fprintf(stderr, ")\n");
351
352         abort();
353 }
354
355 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
356 {
357         fprintf(stderr, "java.lang.VerifyError: ");
358         utf_fprint_printable_ascii(stderr, m->name);
359         fprintf(stderr, ": %s", message);
360
361         abort();
362 }
363
364 void exceptions_throw_nosuchfielderror(classinfo *c, utf *name)
365 {
366         fprintf(stderr, "java.lang.NoSuchFieldError: ");
367         utf_fprint_printable_ascii(stderr, c->name);
368         fprintf(stderr, ".");
369         utf_fprint_printable_ascii(stderr, name);
370         fputc('\n', stderr);
371
372         abort();
373 }
374
375 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
376 {
377         fprintf(stderr, "java.lang.NoSuchMethodError: ");
378         utf_fprint_printable_ascii(stderr, c->name);
379         fprintf(stderr, ".");
380         utf_fprint_printable_ascii(stderr, name);
381         utf_fprint_printable_ascii(stderr, desc);
382         fputc('\n', stderr);
383
384         abort();
385 }
386
387 void exceptions_throw_unsupportedclassversionerror(classinfo *c,
388                                                                                                    const char *message, ...)
389 {
390         va_list ap;
391
392         fprintf(stderr, "java.lang.UnsupportedClassVersionError: " );
393
394         utf_display_printable_ascii(c->name);
395         fprintf(stderr, ": ");
396
397         va_start(ap, message);
398         vfprintf(stderr, message, ap);
399         va_end(ap);
400
401         fputc('\n', stderr);
402
403         abort();
404 }
405
406 void exceptions_throw_classnotfoundexception(utf *name)
407 {
408         fprintf(stderr, "java.lang.ClassNotFoundException: ");
409         utf_fprint_printable_ascii(stderr, name);
410         fputc('\n', stderr);
411
412         abort();
413 }
414
415 void exceptions_throw_nullpointerexception(void)
416 {
417         fprintf(stderr, "java.lang.NullPointerException\n");
418
419         abort();
420 }
421
422
423 /* finalizer ******************************************************************/
424
425 void finalizer_notify(void)
426 {
427         vm_abort("finalizer_notify");
428 }
429
430 void finalizer_run(void *o, void *p)
431 {
432         vm_abort("finalizer_run");
433 }
434
435
436 /* gc *************************************************************************/
437
438 void gc_reference_register(java_object_t **ref, int32_t reftype)
439 {
440         vm_abort("gc_reference_register");
441 }
442
443 int64_t gc_get_heap_size(void)
444 {
445         return 0;
446 }
447
448 int64_t gc_get_free_bytes(void)
449 {
450         return 0;
451 }
452
453 int64_t gc_get_total_bytes(void)
454 {
455         return 0;
456 }
457
458 int64_t gc_get_max_heap_size(void)
459 {
460         return 0;
461 }
462
463
464 /* heap ***********************************************************************/
465
466 void *heap_alloc_uncollectable(uint32_t bytelength)
467 {
468         return calloc(bytelength, 1);
469 }
470
471 s4 heap_get_hashcode(java_object_t *o)
472 {
473         return 0;
474 }
475
476
477 /* jit ************************************************************************/
478
479 void jit_invalidate_code(methodinfo *m)
480 {
481         vm_abort("jit_invalidate_code");
482 }
483
484
485 /* llni ***********************************************************************/
486
487 void llni_critical_start()
488 {
489         vm_abort("llni_critical_start");
490 }
491
492 void llni_critical_end()
493 {
494         vm_abort("llni_critical_end");
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_object_t *o)
521 {
522         return true;
523 }
524
525 bool lock_monitor_exit(java_object_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 *dump_alloc(int32_t size)
562 {
563         return malloc(size);
564 }
565
566 void dump_release(int32_t size)
567 {
568 }
569
570 int32_t dump_size(void)
571 {
572         return 0;
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_arrayclass_get_by_type(int type)
588 {
589         return NULL;
590 }
591
592 classinfo *primitive_class_get_by_type(int type)
593 {
594         abort();
595         return NULL;
596 }
597
598 classinfo *primitive_class_get_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 /* resolve ********************************************************************/
618
619 bool resolve_class_from_typedesc(typedesc *d, bool checkaccess, bool link, classinfo **result)
620 {
621         abort();
622
623         return false;
624 }
625
626 /* stupid resolving implementation used by resolve_classref_or_classinfo_eager */
627 /* This function does eager resolving without any access checks.               */
628
629 static classinfo * dummy_resolve_class_from_name(classinfo *referer,
630                                                  utf *classname,
631                                                  bool checkaccess)
632 {
633         classinfo *cls = NULL;
634         char *utf_ptr;
635         int len;
636         
637         assert(referer);
638         assert(classname);
639         
640         /* lookup if this class has already been loaded */
641
642         cls = classcache_lookup(referer->classloader, classname);
643
644         if (!cls) {
645                 /* resolve array types */
646
647                 if (classname->text[0] == '[') {
648                         utf_ptr = classname->text + 1;
649                         len = classname->blength - 1;
650
651                         /* classname is an array type name */
652
653                         switch (*utf_ptr) {
654                                 case 'L':
655                                         utf_ptr++;
656                                         len -= 2;
657                                         /* FALLTHROUGH */
658                                 case '[':
659                                         /* the component type is a reference type */
660                                         /* resolve the component type */
661                                         if ((cls = dummy_resolve_class_from_name(referer,
662                                                                            utf_new(utf_ptr,len),
663                                                                            checkaccess)) == NULL)
664                                                 return NULL; /* exception */
665
666                                         /* create the array class */
667                                         cls = class_array_of(cls,false);
668                                         if (!cls)
669                                                 return NULL; /* exception */
670                         }
671                 }
672
673                 /* load the class */
674                 if (!cls) {
675                         if (!(cls = load_class_from_classloader(classname,
676                                                                                                         referer->classloader)))
677                                 return false; /* exception */
678                 }
679         }
680
681         /* the class is now loaded */
682         assert(cls);
683         assert(cls->state & CLASS_LOADED);
684
685         return cls;
686 }
687
688
689 classinfo * resolve_classref_or_classinfo_eager(classref_or_classinfo cls,
690                                                                                                 bool checkaccess)
691 {
692         classinfo         *c;
693         
694         assert(cls.any);
695
696         if (IS_CLASSREF(cls)) {
697                 /* we must resolve this reference */
698
699                 if ((c = dummy_resolve_class_from_name(cls.ref->referer, cls.ref->name,
700                                                                                    checkaccess)) == NULL)
701                         return NULL;
702         }
703         else {
704                 /* cls has already been resolved */
705                 c = cls.cls;
706         }
707
708         assert(c);
709         assert(c->state & CLASS_LOADED);
710
711         /* succeeded */
712         return c;
713 }
714
715
716 /* stacktrace *****************************************************************/
717
718 java_handle_objectarray_t *stacktrace_getClassContext()
719 {
720         return NULL;
721 }
722
723
724 /* threads ********************************************************************/
725
726 intptr_t threads_get_current_tid(void)
727 {
728         return 0;
729 }
730
731 void threads_cast_stopworld(void)
732 {
733 }
734
735 void threads_cast_startworld(void)
736 {
737 }
738
739
740 /* vm *************************************************************************/
741
742 void vm_printconfig(void)
743 {
744 }
745
746 void vm_abort(const char *text, ...)
747 {
748         va_list ap;
749
750         /* print the log message */
751
752         va_start(ap, text);
753         vfprintf(stderr, text, ap);
754         va_end(ap);
755
756         /* now abort the VM */
757
758         abort();
759 }
760
761 java_handle_t *vm_call_method(methodinfo *m, java_handle_t *o, ...)
762 {
763         return NULL;
764 }
765
766
767 /* XXX */
768
769 void stringtable_update(void)
770 {
771         log_println("stringtable_update: REMOVE ME!");
772 }
773
774 java_object_t *literalstring_new(utf *u)
775 {
776         log_println("literalstring_new: REMOVE ME!");
777
778         return NULL;
779 }
780
781
782 void print_dynamic_super_statistics(void)
783 {
784 }
785
786
787 /*
788  * These are local overrides for various environment variables in Emacs.
789  * Please do not remove this and leave it at the end of the file, where
790  * Emacs will automagically detect them.
791  * ---------------------------------------------------------------------
792  * Local variables:
793  * mode: c
794  * indent-tabs-mode: t
795  * c-basic-offset: 4
796  * tab-width: 4
797  * End:
798  * vim:noexpandtab:sw=4:ts=4:
799  */