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/globals.hpp"
50 #include "vmcore/utf8.h"
54 * Returns the primitive class of the given class name.
56 * @param name Name of the class.
58 * @return Class structure.
60 classinfo* Primitive::get_class_by_name(utf *name)
64 /* search table of primitive classes */
66 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
67 if (primitivetype_table[i].name == name)
68 return primitivetype_table[i].class_primitive;
70 /* keep compiler happy */
77 * Returns the primitive class of the given type.
79 * @param type Integer type of the class.
81 * @return Class structure.
83 classinfo* Primitive::get_class_by_type(int type)
85 return primitivetype_table[type].class_primitive;
90 * Returns the primitive class of the given type.
94 * @return Class structure.
96 classinfo* Primitive::get_class_by_char(char ch)
102 index = PRIMITIVETYPE_INT;
105 index = PRIMITIVETYPE_LONG;
108 index = PRIMITIVETYPE_FLOAT;
111 index = PRIMITIVETYPE_DOUBLE;
114 index = PRIMITIVETYPE_BYTE;
117 index = PRIMITIVETYPE_CHAR;
120 index = PRIMITIVETYPE_SHORT;
123 index = PRIMITIVETYPE_BOOLEAN;
126 index = PRIMITIVETYPE_VOID;
132 return primitivetype_table[index].class_primitive;
137 * Returns the primitive array-class of the given primitive class
140 * @param name Name of the class.
142 * @return Class structure.
144 classinfo* Primitive::get_arrayclass_by_name(utf *name)
148 /* search table of primitive classes */
150 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
151 if (primitivetype_table[i].name == name)
152 return primitivetype_table[i].arrayclass;
154 /* keep compiler happy */
161 * Returns the primitive array-class of the given type.
163 * @param type Integer type of the class.
165 * @return Class structure.
167 classinfo* Primitive::get_arrayclass_by_type(int type)
169 return primitivetype_table[type].arrayclass;
174 * Returns the primitive type of the given wrapper-class.
176 * @param c Class structure.
178 * @return Integer type of the class.
180 int Primitive::get_type_by_wrapperclass(classinfo *c)
184 /* Search primitive table. */
186 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
187 if (primitivetype_table[i].class_wrap == c)
190 /* Invalid primitive wrapper-class. */
197 * Box a primitive of the given type. If the type is an object,
200 * @param type Type of the passed value.
201 * @param value Value to box.
203 * @return Handle of the boxing Java object.
205 java_handle_t* Primitive::box(int type, imm_union value)
210 case PRIMITIVETYPE_BOOLEAN:
211 o = box((uint8_t) value.i);
213 case PRIMITIVETYPE_BYTE:
214 o = box((int8_t) value.i);
216 case PRIMITIVETYPE_CHAR:
217 o = box((uint16_t) value.i);
219 case PRIMITIVETYPE_SHORT:
220 o = box((int16_t) value.i);
222 case PRIMITIVETYPE_INT:
225 case PRIMITIVETYPE_LONG:
228 case PRIMITIVETYPE_FLOAT:
231 case PRIMITIVETYPE_DOUBLE:
234 case PRIMITIVETYPE_VOID:
235 o = (java_handle_t*) value.a;
239 vm_abort("primitive_box: invalid primitive type %d", type);
247 * Unbox a primitive of the given type. If the type is an object,
250 * @param h Handle of the Java object.
252 * @return Unboxed value as union.
254 imm_union Primitive::unbox(java_handle_t *h)
265 LLNI_class_get(h, c);
267 type = get_type_by_wrapperclass(c);
270 case PRIMITIVETYPE_BOOLEAN:
271 value.i = unbox_boolean(h);
273 case PRIMITIVETYPE_BYTE:
274 value.i = unbox_byte(h);
276 case PRIMITIVETYPE_CHAR:
277 value.i = unbox_char(h);
279 case PRIMITIVETYPE_SHORT:
280 value.i = unbox_short(h);
282 case PRIMITIVETYPE_INT:
283 value.i = unbox_int(h);
285 case PRIMITIVETYPE_LONG:
286 value.l = unbox_long(h);
288 case PRIMITIVETYPE_FLOAT:
289 value.f = unbox_float(h);
291 case PRIMITIVETYPE_DOUBLE:
292 value.d = unbox_double(h);
295 /* If type is -1 the object is not a primitive box but a
300 vm_abort("Primitive::unbox: invalid primitive type %d", type);
307 /* primitive_box_xxx ***********************************************************
309 Box a primitive type.
311 *******************************************************************************/
313 #define PRIMITIVE_BOX_TYPE(name, object, type) \
314 java_handle_t* Primitive::box(type value) \
317 java_lang_##object *jo; \
319 o = builtin_new(class_java_lang_##object); \
324 jo = (java_lang_##object *) o; \
326 LLNI_field_set_val(jo, value, value); \
331 PRIMITIVE_BOX_TYPE(boolean, Boolean, uint8_t)
332 PRIMITIVE_BOX_TYPE(byte, Byte, int8_t)
333 PRIMITIVE_BOX_TYPE(char, Character, uint16_t)
334 PRIMITIVE_BOX_TYPE(short, Short, int16_t)
335 PRIMITIVE_BOX_TYPE(int, Integer, int32_t)
336 PRIMITIVE_BOX_TYPE(long, Long, int64_t)
337 PRIMITIVE_BOX_TYPE(float, Float, float)
338 PRIMITIVE_BOX_TYPE(double, Double, double)
341 /* primitive_unbox_xxx *********************************************************
343 Unbox a primitive type.
345 *******************************************************************************/
347 #define PRIMITIVE_UNBOX_TYPE(name, object, type) \
348 type Primitive::unbox_##name(java_handle_t *h) \
350 java_lang_##object *jo; \
353 jo = (java_lang_##object *) h; \
355 LLNI_field_get_val(jo, value, value); \
360 PRIMITIVE_UNBOX_TYPE(boolean, Boolean, uint8_t)
361 PRIMITIVE_UNBOX_TYPE(byte, Byte, int8_t)
362 PRIMITIVE_UNBOX_TYPE(char, Character, uint16_t)
363 PRIMITIVE_UNBOX_TYPE(short, Short, int16_t)
364 PRIMITIVE_UNBOX_TYPE(int, Integer, int32_t)
365 PRIMITIVE_UNBOX_TYPE(long, Long, int64_t)
366 PRIMITIVE_UNBOX_TYPE(float, Float, float)
367 PRIMITIVE_UNBOX_TYPE(double, Double, double)
370 // Legacy C interface.
374 classinfo* Primitive_get_class_by_name(utf *name) { return Primitive::get_class_by_name(name); }
375 classinfo* Primitive_get_class_by_type(int type) { return Primitive::get_class_by_type(type); }
376 classinfo* Primitive_get_class_by_char(char ch) { return Primitive::get_class_by_char(ch); }
377 classinfo* Primitive_get_arrayclass_by_name(utf *name) { return Primitive::get_arrayclass_by_name(name); }
378 classinfo* Primitive_get_arrayclass_by_type(int type) { return Primitive::get_arrayclass_by_type(type); }
379 int Primitive_get_type_by_wrapperclass(classinfo *c) { return Primitive::get_type_by_wrapperclass(c); }
380 java_handle_t* Primitive_box(int type, imm_union value) { return Primitive::box(type, value); }
381 imm_union Primitive_unbox(java_handle_t *h) { return Primitive::unbox(h); }
386 * These are local overrides for various environment variables in Emacs.
387 * Please do not remove this and leave it at the end of the file, where
388 * Emacs will automagically detect them.
389 * ---------------------------------------------------------------------
392 * indent-tabs-mode: t
396 * vim:noexpandtab:sw=4:ts=4: