81a0d34fcce5f6406d8312d222c70e0f0f0b54bb
[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.
196
197 *******************************************************************************/
198
199 java_handle_t *primitive_box(int type, imm_union value)
200 {
201         java_handle_t *o;
202
203         switch (type) {
204         case PRIMITIVETYPE_BOOLEAN:
205                 o = primitive_box_boolean(value.i);
206                 break;
207         case PRIMITIVETYPE_BYTE:
208                 o = primitive_box_byte(value.i);
209                 break;
210         case PRIMITIVETYPE_CHAR:
211                 o = primitive_box_char(value.i);
212                 break;
213         case PRIMITIVETYPE_SHORT:
214                 o = primitive_box_short(value.i);
215                 break;
216         case PRIMITIVETYPE_INT:
217                 o = primitive_box_int(value.i);
218                 break;
219         case PRIMITIVETYPE_LONG:
220                 o = primitive_box_long(value.l);
221                 break;
222         case PRIMITIVETYPE_FLOAT:
223                 o = primitive_box_float(value.f);
224                 break;
225         case PRIMITIVETYPE_DOUBLE:
226                 o = primitive_box_double(value.d);
227                 break;
228         default:
229                 vm_abort("primitive_box: invalid primitive type %d", type);
230         }
231
232         return o;
233 }
234
235
236 /* primitive_unbox *************************************************************
237
238    Unbox a primitive of the given type.
239
240 *******************************************************************************/
241
242 imm_union primitive_unbox(java_handle_t *o)
243 {
244         classinfo *c;
245         int        type;
246         imm_union  value;
247
248         c = o->vftbl->class;
249
250         type = primitive_type_get_by_wrapperclass(c);
251
252         switch (type) {
253         case PRIMITIVETYPE_BOOLEAN:
254                 value.i = primitive_unbox_boolean(o);
255                 break;
256         case PRIMITIVETYPE_BYTE:
257                 value.i = primitive_unbox_byte(o);
258                 break;
259         case PRIMITIVETYPE_CHAR:
260                 value.i = primitive_unbox_char(o);
261                 break;
262         case PRIMITIVETYPE_SHORT:
263                 value.i = primitive_unbox_short(o);
264                 break;
265         case PRIMITIVETYPE_INT:
266                 value.i = primitive_unbox_int(o);
267                 break;
268         case PRIMITIVETYPE_LONG:
269                 value.l = primitive_unbox_long(o);
270                 break;
271         case PRIMITIVETYPE_FLOAT:
272                 value.f = primitive_unbox_float(o);
273                 break;
274         case PRIMITIVETYPE_DOUBLE:
275                 value.d = primitive_unbox_double(o);
276                 break;
277         default:
278                 vm_abort("primitive_unbox: invalid primitive type %d", type);
279         }
280
281         return value;
282 }
283
284
285 /* primitive_box_xxx ***********************************************************
286
287    Box a primitive type.
288
289 *******************************************************************************/
290
291 #define PRIMITIVE_BOX_TYPE(name, object, type)      \
292 java_handle_t *primitive_box_##name(type value)     \
293 {                                                   \
294         java_handle_t      *o;                          \
295         java_lang_##object *jo;                         \
296                                                     \
297         o = builtin_new(class_java_lang_##object);      \
298                                                     \
299         if (o == NULL)                                  \
300                 return NULL;                                \
301                                                     \
302         jo = (java_lang_##object *) o;                  \
303                                                     \
304         LLNI_field_set_val(jo, value, value);           \
305                                                     \
306         return o;                                       \
307 }
308
309 PRIMITIVE_BOX_TYPE(boolean, Boolean,   int32_t)
310 PRIMITIVE_BOX_TYPE(byte,    Byte,      int32_t)
311 PRIMITIVE_BOX_TYPE(char,    Character, int32_t)
312 PRIMITIVE_BOX_TYPE(short,   Short,     int32_t)
313 PRIMITIVE_BOX_TYPE(int,     Integer,   int32_t)
314 PRIMITIVE_BOX_TYPE(long,    Long,      int64_t)
315 PRIMITIVE_BOX_TYPE(float,   Float,     float)
316 PRIMITIVE_BOX_TYPE(double,  Double,    double)
317
318
319 /* primitive_unbox_xxx *********************************************************
320
321    Unbox a primitive type.
322
323 *******************************************************************************/
324
325 #define PRIMITIVE_UNBOX_TYPE(name, object, type)  \
326 type primitive_unbox_##name(java_handle_t *o)     \
327 {                                                 \
328         java_lang_##object *jo;                       \
329         type                value;                    \
330                                                   \
331         jo = (java_lang_##object *) o;                \
332                                                   \
333         LLNI_field_get_val(jo, value, value);         \
334                                                   \
335         return value;                                 \
336 }
337
338 PRIMITIVE_UNBOX_TYPE(boolean, Boolean,   int32_t)
339 PRIMITIVE_UNBOX_TYPE(byte,    Byte,      int32_t)
340 PRIMITIVE_UNBOX_TYPE(char,    Character, int32_t)
341 PRIMITIVE_UNBOX_TYPE(short,   Short,     int32_t)
342 PRIMITIVE_UNBOX_TYPE(int,     Integer,   int32_t)
343 PRIMITIVE_UNBOX_TYPE(long,    Long,      int64_t)
344 PRIMITIVE_UNBOX_TYPE(float,   Float,     float)
345 PRIMITIVE_UNBOX_TYPE(double,  Double,    double)
346
347
348 /*
349  * These are local overrides for various environment variables in Emacs.
350  * Please do not remove this and leave it at the end of the file, where
351  * Emacs will automagically detect them.
352  * ---------------------------------------------------------------------
353  * Local variables:
354  * mode: c
355  * indent-tabs-mode: t
356  * c-basic-offset: 4
357  * tab-width: 4
358  * End:
359  * vim:noexpandtab:sw=4:ts=4:
360  */