GNU header update.
[cacao.git] / src / native / vm / VMSystem.c
1 /* native/vm/VMSystem.c - java/lang/VMSystem
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30
31    $Id: VMSystem.c 1735 2004-12-07 14:33:27Z twisti $
32
33 */
34
35
36 #include <string.h>
37 #include <time.h>
38
39 #include "native/jni.h"
40 #include "native/native.h"
41 #include "native/include/java_lang_Object.h"
42 #include "toolbox/logging.h"
43 #include "vm/exceptions.h"
44 #include "vm/builtin.h"
45
46
47 /*
48  * Class:     java/lang/System
49  * Method:    arraycopy
50  * Signature: (Ljava/lang/Object;ILjava/lang/Object;II)V
51  */
52 JNIEXPORT void JNICALL Java_java_lang_VMSystem_arraycopy(JNIEnv *env, jclass clazz, java_lang_Object *source, s4 sp, java_lang_Object *dest, s4 dp, s4 len)
53 {
54         s4 i;
55         java_arrayheader *s = (java_arrayheader *) source;
56         java_arrayheader *d = (java_arrayheader *) dest;
57         arraydescriptor *sdesc;
58         arraydescriptor *ddesc;
59
60         /*      printf("arraycopy: %p:%x->%p:%x\n",source,sp,dest,dp);
61                 fflush(stdout);*/
62
63         if (!s || !d) { 
64                 *exceptionptr = new_exception(string_java_lang_NullPointerException);
65                 return; 
66         }
67
68         sdesc = s->objheader.vftbl->arraydesc;
69         ddesc = d->objheader.vftbl->arraydesc;
70
71         if (!sdesc || !ddesc || (sdesc->arraytype != ddesc->arraytype)) {
72                 *exceptionptr = new_exception(string_java_lang_ArrayStoreException);
73                 return; 
74         }
75
76         if ((len < 0) || (sp < 0) || (sp + len > s->size) || (dp < 0) || (dp + len > d->size)) {
77                 *exceptionptr = new_exception(string_java_lang_ArrayIndexOutOfBoundsException);
78                 return; 
79         }
80
81         if (sdesc->componentvftbl == ddesc->componentvftbl) {
82                 /* We copy primitive values or references of exactly the same type */
83                 s4 dataoffset = sdesc->dataoffset;
84                 s4 componentsize = sdesc->componentsize;
85                 memmove(((u1 *) d) + dataoffset + componentsize * dp,
86                                 ((u1 *) s) + dataoffset + componentsize * sp,
87                                 (size_t) len * componentsize);
88
89         } else {
90                 /* We copy references of different type */
91                 java_objectarray *oas = (java_objectarray *) s;
92                 java_objectarray *oad = (java_objectarray *) d;
93                 
94                 if (dp <= sp) {
95                         for (i = 0; i < len; i++) {
96                                 java_objectheader *o = oas->data[sp + i];
97                                 if (!builtin_canstore(oad, o)) {
98                                         *exceptionptr = new_exception(string_java_lang_ArrayStoreException);
99                                         return;
100                                 }
101                                 oad->data[dp + i] = o;
102                         }
103
104                 } else {
105                         /* XXX this does not completely obey the specification!
106                          * If an exception is thrown only the elements above
107                          * the current index have been copied. The
108                          * specification requires that only the elements
109                          * *below* the current index have been copied before
110                          * the throw.
111                          */
112                         for (i = len - 1; i >= 0; i--) {
113                                 java_objectheader *o = oas->data[sp + i];
114                                 if (!builtin_canstore(oad, o)) {
115                                         *exceptionptr = new_exception(string_java_lang_ArrayStoreException);
116                                         return;
117                                 }
118                                 oad->data[dp + i] = o;
119                         }
120                 }
121         }
122 }
123
124
125 /*
126  * Class:     java/lang/System
127  * Method:    identityHashCode
128  * Signature: (Ljava/lang/Object;)I
129  */
130 JNIEXPORT s4 JNICALL Java_java_lang_VMSystem_identityHashCode(JNIEnv *env, jclass clazz, java_lang_Object *par1)
131 {
132         return ((char *) par1) - ((char *) 0);
133 }
134
135
136 /*
137  * These are local overrides for various environment variables in Emacs.
138  * Please do not remove this and leave it at the end of the file, where
139  * Emacs will automagically detect them.
140  * ---------------------------------------------------------------------
141  * Local variables:
142  * mode: c
143  * indent-tabs-mode: t
144  * c-basic-offset: 4
145  * tab-width: 4
146  * End:
147  */