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