1 /* src/vm/primitive.cpp - primitive types
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #include "native/jni.h"
32 #include "native/llni.h"
34 #include "native/include/java_lang_Boolean.h"
35 #include "native/include/java_lang_Byte.h"
36 #include "native/include/java_lang_Short.h"
37 #include "native/include/java_lang_Character.h"
38 #include "native/include/java_lang_Integer.h"
39 #include "native/include/java_lang_Long.h"
40 #include "native/include/java_lang_Float.h"
41 #include "native/include/java_lang_Double.h"
43 #include "vm/builtin.h"
44 #include "vm/global.h"
45 #include "vm/primitive.hpp"
48 #include "vmcore/class.h"
49 #include "vmcore/utf8.h"
53 * Returns the primitive class of the given class name.
55 * @param name Name of the class.
57 * @return Class structure.
59 classinfo* Primitive::get_class_by_name(utf *name)
63 /* search table of primitive classes */
65 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
66 if (primitivetype_table[i].name == name)
67 return primitivetype_table[i].class_primitive;
69 /* keep compiler happy */
76 * Returns the primitive class of the given type.
78 * @param type Integer type of the class.
80 * @return Class structure.
82 classinfo* Primitive::get_class_by_type(int type)
84 return primitivetype_table[type].class_primitive;
89 * Returns the primitive class of the given type.
93 * @return Class structure.
95 classinfo* Primitive::get_class_by_char(char ch)
101 index = PRIMITIVETYPE_INT;
104 index = PRIMITIVETYPE_LONG;
107 index = PRIMITIVETYPE_FLOAT;
110 index = PRIMITIVETYPE_DOUBLE;
113 index = PRIMITIVETYPE_BYTE;
116 index = PRIMITIVETYPE_CHAR;
119 index = PRIMITIVETYPE_SHORT;
122 index = PRIMITIVETYPE_BOOLEAN;
125 index = PRIMITIVETYPE_VOID;
131 return primitivetype_table[index].class_primitive;
136 * Returns the primitive array-class of the given primitive class
139 * @param name Name of the class.
141 * @return Class structure.
143 classinfo* Primitive::get_arrayclass_by_name(utf *name)
147 /* search table of primitive classes */
149 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
150 if (primitivetype_table[i].name == name)
151 return primitivetype_table[i].arrayclass;
153 /* keep compiler happy */
160 * Returns the primitive array-class of the given type.
162 * @param type Integer type of the class.
164 * @return Class structure.
166 classinfo* Primitive::get_arrayclass_by_type(int type)
168 return primitivetype_table[type].arrayclass;
173 * Returns the primitive type of the given wrapper-class.
175 * @param c Class structure.
177 * @return Integer type of the class.
179 int Primitive::get_type_by_wrapperclass(classinfo *c)
183 /* Search primitive table. */
185 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
186 if (primitivetype_table[i].class_wrap == c)
189 /* Invalid primitive wrapper-class. */
196 * Box a primitive of the given type. If the type is an object,
199 * @param type Type of the passed value.
200 * @param value Value to box.
202 * @return Handle of the boxing Java object.
204 java_handle_t* Primitive::box(int type, imm_union value)
209 case PRIMITIVETYPE_BOOLEAN:
210 o = box((uint8_t) value.i);
212 case PRIMITIVETYPE_BYTE:
213 o = box((int8_t) value.i);
215 case PRIMITIVETYPE_CHAR:
216 o = box((uint16_t) value.i);
218 case PRIMITIVETYPE_SHORT:
219 o = box((int16_t) value.i);
221 case PRIMITIVETYPE_INT:
224 case PRIMITIVETYPE_LONG:
227 case PRIMITIVETYPE_FLOAT:
230 case PRIMITIVETYPE_DOUBLE:
233 case PRIMITIVETYPE_VOID:
234 o = (java_handle_t*) value.a;
238 vm_abort("primitive_box: invalid primitive type %d", type);
246 * Unbox a primitive of the given type. If the type is an object,
249 * @param h Handle of the Java object.
251 * @return Unboxed value as union.
253 imm_union Primitive::unbox(java_handle_t *h)
264 LLNI_class_get(h, c);
266 type = get_type_by_wrapperclass(c);
269 case PRIMITIVETYPE_BOOLEAN:
270 value.i = unbox_boolean(h);
272 case PRIMITIVETYPE_BYTE:
273 value.i = unbox_byte(h);
275 case PRIMITIVETYPE_CHAR:
276 value.i = unbox_char(h);
278 case PRIMITIVETYPE_SHORT:
279 value.i = unbox_short(h);
281 case PRIMITIVETYPE_INT:
282 value.i = unbox_int(h);
284 case PRIMITIVETYPE_LONG:
285 value.l = unbox_long(h);
287 case PRIMITIVETYPE_FLOAT:
288 value.f = unbox_float(h);
290 case PRIMITIVETYPE_DOUBLE:
291 value.d = unbox_double(h);
294 /* If type is -1 the object is not a primitive box but a
299 vm_abort("Primitive::unbox: invalid primitive type %d", type);
306 /* primitive_box_xxx ***********************************************************
308 Box a primitive type.
310 *******************************************************************************/
312 #define PRIMITIVE_BOX_TYPE(name, object, type) \
313 java_handle_t* Primitive::box(type value) \
316 java_lang_##object *jo; \
318 o = builtin_new(class_java_lang_##object); \
323 jo = (java_lang_##object *) o; \
325 LLNI_field_set_val(jo, value, value); \
330 PRIMITIVE_BOX_TYPE(boolean, Boolean, uint8_t)
331 PRIMITIVE_BOX_TYPE(byte, Byte, int8_t)
332 PRIMITIVE_BOX_TYPE(char, Character, uint16_t)
333 PRIMITIVE_BOX_TYPE(short, Short, int16_t)
334 PRIMITIVE_BOX_TYPE(int, Integer, int32_t)
335 PRIMITIVE_BOX_TYPE(long, Long, int64_t)
336 PRIMITIVE_BOX_TYPE(float, Float, float)
337 PRIMITIVE_BOX_TYPE(double, Double, double)
340 /* primitive_unbox_xxx *********************************************************
342 Unbox a primitive type.
344 *******************************************************************************/
346 #define PRIMITIVE_UNBOX_TYPE(name, object, type) \
347 type Primitive::unbox_##name(java_handle_t *h) \
349 java_lang_##object *jo; \
352 jo = (java_lang_##object *) h; \
354 LLNI_field_get_val(jo, value, value); \
359 PRIMITIVE_UNBOX_TYPE(boolean, Boolean, uint8_t)
360 PRIMITIVE_UNBOX_TYPE(byte, Byte, int8_t)
361 PRIMITIVE_UNBOX_TYPE(char, Character, uint16_t)
362 PRIMITIVE_UNBOX_TYPE(short, Short, int16_t)
363 PRIMITIVE_UNBOX_TYPE(int, Integer, int32_t)
364 PRIMITIVE_UNBOX_TYPE(long, Long, int64_t)
365 PRIMITIVE_UNBOX_TYPE(float, Float, float)
366 PRIMITIVE_UNBOX_TYPE(double, Double, double)
369 // Legacy C interface.
373 classinfo* Primitive_get_class_by_name(utf *name) { return Primitive::get_class_by_name(name); }
374 classinfo* Primitive_get_class_by_type(int type) { return Primitive::get_class_by_type(type); }
375 classinfo* Primitive_get_class_by_char(char ch) { return Primitive::get_class_by_char(ch); }
376 classinfo* Primitive_get_arrayclass_by_name(utf *name) { return Primitive::get_arrayclass_by_name(name); }
377 classinfo* Primitive_get_arrayclass_by_type(int type) { return Primitive::get_arrayclass_by_type(type); }
378 int Primitive_get_type_by_wrapperclass(classinfo *c) { return Primitive::get_type_by_wrapperclass(c); }
379 java_handle_t* Primitive_box(int type, imm_union value) { return Primitive::box(type, value); }
380 imm_union Primitive_unbox(java_handle_t *h) { return Primitive::unbox(h); }
385 * These are local overrides for various environment variables in Emacs.
386 * Please do not remove this and leave it at the end of the file, where
387 * Emacs will automagically detect them.
388 * ---------------------------------------------------------------------
391 * indent-tabs-mode: t
395 * vim:noexpandtab:sw=4:ts=4: