0b59229d10e5e12a59c25db546f5ce48a33fd36c
[cacao.git] / src / vm / primitive.c
1 /* src/vm/primitive.c - primitive types
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    $Id: linker.c 8042 2007-06-07 17:43:29Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <stdint.h>
34
35 #include "native/jni.h"
36 #include "native/llni.h"
37
38 #include "native/include/java_lang_Boolean.h"
39 #include "native/include/java_lang_Byte.h"
40 #include "native/include/java_lang_Short.h"
41 #include "native/include/java_lang_Character.h"
42 #include "native/include/java_lang_Integer.h"
43 #include "native/include/java_lang_Long.h"
44 #include "native/include/java_lang_Float.h"
45 #include "native/include/java_lang_Double.h"
46
47 #include "vm/builtin.h"
48 #include "vm/global.h"
49 #include "vm/primitive.h"
50 #include "vm/vm.h"
51
52 #include "vmcore/class.h"
53 #include "vmcore/utf8.h"
54
55
56 /* primitive_class_get_by_name *************************************************
57
58    Returns the primitive class of the given class name.
59
60 *******************************************************************************/
61
62 classinfo *primitive_class_get_by_name(utf *name)
63 {
64         int i;
65
66         /* search table of primitive classes */
67
68         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
69                 if (primitivetype_table[i].name == name)
70                         return primitivetype_table[i].class_primitive;
71
72         /* keep compiler happy */
73
74         return NULL;
75 }
76
77
78 /* primitive_class_get_by_type *************************************************
79
80    Returns the primitive class of the given type.
81
82 *******************************************************************************/
83
84 classinfo *primitive_class_get_by_type(int type)
85 {
86         return primitivetype_table[type].class_primitive;
87 }
88
89
90 /* primitive_class_get_by_char *************************************************
91
92    Returns the primitive class of the given type.
93
94 *******************************************************************************/
95
96 classinfo *primitive_class_get_by_char(char ch)
97 {
98         int index;
99
100         switch (ch) {
101         case 'I':
102                 index = PRIMITIVETYPE_INT;
103                 break;
104         case 'J':
105                 index = PRIMITIVETYPE_LONG;
106                 break;
107         case 'F':
108                 index = PRIMITIVETYPE_FLOAT;
109                 break;
110         case 'D':
111                 index = PRIMITIVETYPE_DOUBLE;
112                 break;
113         case 'B':
114                 index = PRIMITIVETYPE_BYTE;
115                 break;
116         case 'C':
117                 index = PRIMITIVETYPE_CHAR;
118                 break;
119         case 'S':
120                 index = PRIMITIVETYPE_SHORT;
121                 break;
122         case 'Z':
123                 index = PRIMITIVETYPE_BOOLEAN;
124                 break;
125         case 'V':
126                 index = PRIMITIVETYPE_VOID;
127                 break;
128         default:
129                 return NULL;
130         }
131
132         return primitivetype_table[index].class_primitive;
133 }
134
135
136 /* primitive_arrayclass_get_by_name ********************************************
137
138    Returns the primitive array-class of the given primitive class
139    name.
140
141 *******************************************************************************/
142
143 classinfo *primitive_arrayclass_get_by_name(utf *name)
144 {
145         int i;
146
147         /* search table of primitive classes */
148
149         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
150                 if (primitivetype_table[i].name == name)
151                         return primitivetype_table[i].arrayclass;
152
153         /* keep compiler happy */
154
155         return NULL;
156 }
157
158
159 /* primitive_arrayclass_get_by_type ********************************************
160
161    Returns the primitive array-class of the given type.
162
163 *******************************************************************************/
164
165 classinfo *primitive_arrayclass_get_by_type(int type)
166 {
167         return primitivetype_table[type].arrayclass;
168 }
169
170
171 /* primitive_type_get_by_wrapperclass ******************************************
172
173    Returns the primitive type of the given wrapper-class.
174
175 *******************************************************************************/
176
177 int primitive_type_get_by_wrapperclass(classinfo *c)
178 {
179         int i;
180
181         /* Search primitive table. */
182
183         for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
184                 if (primitivetype_table[i].class_wrap == c)
185                         return i;
186
187         /* Invalid primitive wrapper-class. */
188
189         return -1;
190 }
191
192
193 /* primitive_box ***************************************************************
194
195    Box a primitive of the given type.  If the type is an object,
196    simply return it.
197
198 *******************************************************************************/
199
200 java_handle_t *primitive_box(int type, imm_union value)
201 {
202         java_handle_t *o;
203
204         switch (type) {
205         case PRIMITIVETYPE_BOOLEAN:
206                 o = primitive_box_boolean(value.i);
207                 break;
208         case PRIMITIVETYPE_BYTE:
209                 o = primitive_box_byte(value.i);
210                 break;
211         case PRIMITIVETYPE_CHAR:
212                 o = primitive_box_char(value.i);
213                 break;
214         case PRIMITIVETYPE_SHORT:
215                 o = primitive_box_short(value.i);
216                 break;
217         case PRIMITIVETYPE_INT:
218                 o = primitive_box_int(value.i);
219                 break;
220         case PRIMITIVETYPE_LONG:
221                 o = primitive_box_long(value.l);
222                 break;
223         case PRIMITIVETYPE_FLOAT:
224                 o = primitive_box_float(value.f);
225                 break;
226         case PRIMITIVETYPE_DOUBLE:
227                 o = primitive_box_double(value.d);
228                 break;
229         case PRIMITIVETYPE_VOID:
230                 o = value.a;
231                 break;
232         default:
233                 vm_abort("primitive_box: invalid primitive type %d", type);
234         }
235
236         return o;
237 }
238
239
240 /* primitive_unbox *************************************************************
241
242    Unbox a primitive of the given type.  If the type is an object,
243    simply return it.
244
245 *******************************************************************************/
246
247 imm_union primitive_unbox(java_handle_t *o)
248 {
249         classinfo *c;
250         int        type;
251         imm_union  value;
252
253         c = o->vftbl->class;
254
255         type = primitive_type_get_by_wrapperclass(c);
256
257         switch (type) {
258         case PRIMITIVETYPE_BOOLEAN:
259                 value.i = primitive_unbox_boolean(o);
260                 break;
261         case PRIMITIVETYPE_BYTE:
262                 value.i = primitive_unbox_byte(o);
263                 break;
264         case PRIMITIVETYPE_CHAR:
265                 value.i = primitive_unbox_char(o);
266                 break;
267         case PRIMITIVETYPE_SHORT:
268                 value.i = primitive_unbox_short(o);
269                 break;
270         case PRIMITIVETYPE_INT:
271                 value.i = primitive_unbox_int(o);
272                 break;
273         case PRIMITIVETYPE_LONG:
274                 value.l = primitive_unbox_long(o);
275                 break;
276         case PRIMITIVETYPE_FLOAT:
277                 value.f = primitive_unbox_float(o);
278                 break;
279         case PRIMITIVETYPE_DOUBLE:
280                 value.d = primitive_unbox_double(o);
281                 break;
282         case -1:
283                 /* If type is -1 the object is not a primitive box but a
284                    normal object. */
285                 value.a = o;
286                 break;
287         default:
288                 vm_abort("primitive_unbox: invalid primitive type %d", type);
289         }
290
291         return value;
292 }
293
294
295 /* primitive_box_xxx ***********************************************************
296
297    Box a primitive type.
298
299 *******************************************************************************/
300
301 #define PRIMITIVE_BOX_TYPE(name, object, type)      \
302 java_handle_t *primitive_box_##name(type value)     \
303 {                                                   \
304         java_handle_t      *o;                          \
305         java_lang_##object *jo;                         \
306                                                     \
307         o = builtin_new(class_java_lang_##object);      \
308                                                     \
309         if (o == NULL)                                  \
310                 return NULL;                                \
311                                                     \
312         jo = (java_lang_##object *) o;                  \
313                                                     \
314         LLNI_field_set_val(jo, value, value);           \
315                                                     \
316         return o;                                       \
317 }
318
319 PRIMITIVE_BOX_TYPE(boolean, Boolean,   int32_t)
320 PRIMITIVE_BOX_TYPE(byte,    Byte,      int32_t)
321 PRIMITIVE_BOX_TYPE(char,    Character, int32_t)
322 PRIMITIVE_BOX_TYPE(short,   Short,     int32_t)
323 PRIMITIVE_BOX_TYPE(int,     Integer,   int32_t)
324 PRIMITIVE_BOX_TYPE(long,    Long,      int64_t)
325 PRIMITIVE_BOX_TYPE(float,   Float,     float)
326 PRIMITIVE_BOX_TYPE(double,  Double,    double)
327
328
329 /* primitive_unbox_xxx *********************************************************
330
331    Unbox a primitive type.
332
333 *******************************************************************************/
334
335 #define PRIMITIVE_UNBOX_TYPE(name, object, type)  \
336 type primitive_unbox_##name(java_handle_t *o)     \
337 {                                                 \
338         java_lang_##object *jo;                       \
339         type                value;                    \
340                                                   \
341         jo = (java_lang_##object *) o;                \
342                                                   \
343         LLNI_field_get_val(jo, value, value);         \
344                                                   \
345         return value;                                 \
346 }
347
348 PRIMITIVE_UNBOX_TYPE(boolean, Boolean,   int32_t)
349 PRIMITIVE_UNBOX_TYPE(byte,    Byte,      int32_t)
350 PRIMITIVE_UNBOX_TYPE(char,    Character, int32_t)
351 PRIMITIVE_UNBOX_TYPE(short,   Short,     int32_t)
352 PRIMITIVE_UNBOX_TYPE(int,     Integer,   int32_t)
353 PRIMITIVE_UNBOX_TYPE(long,    Long,      int64_t)
354 PRIMITIVE_UNBOX_TYPE(float,   Float,     float)
355 PRIMITIVE_UNBOX_TYPE(double,  Double,    double)
356
357
358 /*
359  * These are local overrides for various environment variables in Emacs.
360  * Please do not remove this and leave it at the end of the file, where
361  * Emacs will automagically detect them.
362  * ---------------------------------------------------------------------
363  * Local variables:
364  * mode: c
365  * indent-tabs-mode: t
366  * c-basic-offset: 4
367  * tab-width: 4
368  * End:
369  * vim:noexpandtab:sw=4:ts=4:
370  */