717230e65202682b004c7006d1850176bd7b8929
[cacao.git] / src / native / vm / VMSystem.c
1 /* src/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             Christian Thalinger
31
32    $Id: VMSystem.c 4125 2006-01-10 20:50:53Z twisti $
33
34 */
35
36
37 #include <string.h>
38
39 #include "config.h"
40 #include "vm/types.h"
41
42 #include "native/jni.h"
43 #include "native/native.h"
44 #include "native/include/java_lang_Object.h"
45 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/stringlocal.h"
49
50
51 /*
52  * Class:     java/lang/VMSystem
53  * Method:    arraycopy
54  * Signature: (Ljava/lang/Object;ILjava/lang/Object;II)V
55  */
56 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)
57 {
58         java_arrayheader *s;
59         java_arrayheader *d;
60         arraydescriptor  *sdesc;
61         arraydescriptor  *ddesc;
62         s4                i;
63
64         s = (java_arrayheader *) source;
65         d = (java_arrayheader *) dest;
66
67         if (!s || !d) { 
68                 exceptions_throw_nullpointerexception();
69                 return; 
70         }
71
72         sdesc = s->objheader.vftbl->arraydesc;
73         ddesc = d->objheader.vftbl->arraydesc;
74
75         if (!sdesc || !ddesc || (sdesc->arraytype != ddesc->arraytype)) {
76                 *exceptionptr = new_arraystoreexception();
77                 return; 
78         }
79
80         /* we try to throw exception with the same message as SUN does */
81
82         if ((len < 0) || (sp < 0) || (dp < 0) ||
83                 (sp + len < 0) || (sp + len > s->size) ||
84                 (dp + len < 0) || (dp + len > d->size)) {
85                 exceptions_throw_arrayindexoutofboundsexception();
86                 return; 
87         }
88
89         if (sdesc->componentvftbl == ddesc->componentvftbl) {
90                 /* We copy primitive values or references of exactly the same type */
91
92                 s4 dataoffset = sdesc->dataoffset;
93                 s4 componentsize = sdesc->componentsize;
94
95                 memmove(((u1 *) d) + dataoffset + componentsize * dp,
96                                 ((u1 *) s) + dataoffset + componentsize * sp,
97                                 (size_t) len * componentsize);
98
99         } else {
100                 /* We copy references of different type */
101
102                 java_objectarray *oas = (java_objectarray *) s;
103                 java_objectarray *oad = (java_objectarray *) d;
104                 
105                 if (dp <= sp) {
106                         for (i = 0; i < len; i++) {
107                                 java_objectheader *o = oas->data[sp + i];
108                                 if (!builtin_canstore(oad, o)) {
109                                         *exceptionptr = new_arraystoreexception();
110                                         return;
111                                 }
112                                 oad->data[dp + i] = o;
113                         }
114
115                 } else {
116                         /* XXX this does not completely obey the specification!
117                            If an exception is thrown only the elements above the
118                            current index have been copied. The specification
119                            requires that only the elements *below* the current
120                            index have been copied before the throw. */
121
122                         for (i = len - 1; i >= 0; i--) {
123                                 java_objectheader *o = oas->data[sp + i];
124
125                                 if (!builtin_canstore(oad, o)) {
126                                         *exceptionptr = new_arraystoreexception();
127                                         return;
128                                 }
129
130                                 oad->data[dp + i] = o;
131                         }
132                 }
133         }
134 }
135
136
137 /*
138  * Class:     java/lang/VMSystem
139  * Method:    identityHashCode
140  * Signature: (Ljava/lang/Object;)I
141  */
142 JNIEXPORT s4 JNICALL Java_java_lang_VMSystem_identityHashCode(JNIEnv *env, jclass clazz, java_lang_Object *par1)
143 {
144         return ((char *) par1) - ((char *) 0);
145 }
146
147
148 /*
149  * These are local overrides for various environment variables in Emacs.
150  * Please do not remove this and leave it at the end of the file, where
151  * Emacs will automagically detect them.
152  * ---------------------------------------------------------------------
153  * Local variables:
154  * mode: c
155  * indent-tabs-mode: t
156  * c-basic-offset: 4
157  * tab-width: 4
158  * End:
159  */