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/llni.h"
33 #include "vm/builtin.h"
35 #include "vm/global.h"
36 #include "vm/globals.hpp"
37 #include "vm/javaobjects.hpp"
38 #include "vm/primitive.hpp"
44 * Returns the primitive class of the given class name.
46 * @param name Name of the class.
48 * @return Class structure.
50 classinfo* Primitive::get_class_by_name(utf *name)
54 /* search table of primitive classes */
56 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
57 if (primitivetype_table[i].name == name)
58 return primitivetype_table[i].class_primitive;
60 /* keep compiler happy */
67 * Returns the primitive class of the given type.
69 * @param type Integer type of the class.
71 * @return Class structure.
73 classinfo* Primitive::get_class_by_type(int type)
75 return primitivetype_table[type].class_primitive;
80 * Returns the primitive class of the given type.
84 * @return Class structure.
86 classinfo* Primitive::get_class_by_char(char ch)
92 index = PRIMITIVETYPE_INT;
95 index = PRIMITIVETYPE_LONG;
98 index = PRIMITIVETYPE_FLOAT;
101 index = PRIMITIVETYPE_DOUBLE;
104 index = PRIMITIVETYPE_BYTE;
107 index = PRIMITIVETYPE_CHAR;
110 index = PRIMITIVETYPE_SHORT;
113 index = PRIMITIVETYPE_BOOLEAN;
116 index = PRIMITIVETYPE_VOID;
122 return primitivetype_table[index].class_primitive;
127 * Returns the primitive array-class of the given primitive class
130 * @param name Name of the class.
132 * @return Class structure.
134 classinfo* Primitive::get_arrayclass_by_name(utf *name)
138 /* search table of primitive classes */
140 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
141 if (primitivetype_table[i].name == name)
142 return primitivetype_table[i].arrayclass;
144 /* keep compiler happy */
151 * Returns the primitive array-class of the given type.
153 * @param type Integer type of the class.
155 * @return Class structure.
157 classinfo* Primitive::get_arrayclass_by_type(int type)
159 return primitivetype_table[type].arrayclass;
164 * Returns the primitive type of the given wrapper-class.
166 * @param c Class structure.
168 * @return Integer type of the class.
170 int Primitive::get_type_by_wrapperclass(classinfo *c)
174 /* Search primitive table. */
176 for (i = 0; i < PRIMITIVETYPE_COUNT; i++)
177 if (primitivetype_table[i].class_wrap == c)
180 /* Invalid primitive wrapper-class. */
187 * Box a primitive of the given type. If the type is an object,
190 * @param type Type of the passed value.
191 * @param value Value to box.
193 * @return Handle of the boxing Java object.
195 java_handle_t* Primitive::box(int type, imm_union value)
200 case PRIMITIVETYPE_BOOLEAN:
201 o = box((uint8_t) value.i);
203 case PRIMITIVETYPE_BYTE:
204 o = box((int8_t) value.i);
206 case PRIMITIVETYPE_CHAR:
207 o = box((uint16_t) value.i);
209 case PRIMITIVETYPE_SHORT:
210 o = box((int16_t) value.i);
212 case PRIMITIVETYPE_INT:
215 case PRIMITIVETYPE_LONG:
218 case PRIMITIVETYPE_FLOAT:
221 case PRIMITIVETYPE_DOUBLE:
224 case PRIMITIVETYPE_VOID:
225 o = (java_handle_t*) value.a;
229 VM::get_current()->abort("primitive_box: invalid primitive type %d", type);
237 * Unbox a primitive of the given type. If the type is an object,
240 * @param h Handle of the Java object.
242 * @return Unboxed value as union.
244 imm_union Primitive::unbox(java_handle_t *h)
255 LLNI_class_get(h, c);
257 type = get_type_by_wrapperclass(c);
260 case PRIMITIVETYPE_BOOLEAN:
261 value.i = unbox_boolean(h);
263 case PRIMITIVETYPE_BYTE:
264 value.i = unbox_byte(h);
266 case PRIMITIVETYPE_CHAR:
267 value.i = unbox_char(h);
269 case PRIMITIVETYPE_SHORT:
270 value.i = unbox_short(h);
272 case PRIMITIVETYPE_INT:
273 value.i = unbox_int(h);
275 case PRIMITIVETYPE_LONG:
276 value.l = unbox_long(h);
278 case PRIMITIVETYPE_FLOAT:
279 value.f = unbox_float(h);
281 case PRIMITIVETYPE_DOUBLE:
282 value.d = unbox_double(h);
285 /* If type is -1 the object is not a primitive box but a
290 VM::get_current()->abort("Primitive::unbox: invalid primitive type %d", type);
298 * Box a primitive type.
300 java_handle_t* Primitive::box(uint8_t value)
302 java_handle_t *h = builtin_new(class_java_lang_Boolean);
307 java_lang_Boolean b(h);
313 java_handle_t* Primitive::box(int8_t value)
315 java_handle_t *h = builtin_new(class_java_lang_Byte);
326 java_handle_t* Primitive::box(uint16_t value)
328 java_handle_t *h = builtin_new(class_java_lang_Character);
333 java_lang_Character c(h);
339 java_handle_t* Primitive::box(int16_t value)
341 java_handle_t *h = builtin_new(class_java_lang_Short);
346 java_lang_Short s(h);
352 java_handle_t* Primitive::box(int32_t value)
354 java_handle_t *h = builtin_new(class_java_lang_Integer);
359 java_lang_Integer i(h);
365 java_handle_t* Primitive::box(int64_t value)
367 java_handle_t *h = builtin_new(class_java_lang_Long);
378 java_handle_t* Primitive::box(float value)
380 java_handle_t *h = builtin_new(class_java_lang_Float);
385 java_lang_Float f(h);
391 java_handle_t* Primitive::box(double value)
393 java_handle_t *h = builtin_new(class_java_lang_Double);
398 java_lang_Double d(h);
407 * Unbox a primitive type.
410 // template<class T> T Primitive::unbox(java_handle_t *h)
412 // return java_lang_Boolean::get_value(h);
415 inline uint8_t Primitive::unbox_boolean(java_handle_t *h)
417 java_lang_Boolean b(h);
418 return b.get_value();
421 inline int8_t Primitive::unbox_byte(java_handle_t *h)
424 return b.get_value();
427 inline uint16_t Primitive::unbox_char(java_handle_t *h)
429 java_lang_Character c(h);
430 return c.get_value();
433 inline int16_t Primitive::unbox_short(java_handle_t *h)
435 java_lang_Short s(h);
436 return s.get_value();
439 inline int32_t Primitive::unbox_int(java_handle_t *h)
441 java_lang_Integer i(h);
442 return i.get_value();
445 inline int64_t Primitive::unbox_long(java_handle_t *h)
448 return l.get_value();
451 inline float Primitive::unbox_float(java_handle_t *h)
453 java_lang_Float f(h);
454 return f.get_value();
457 inline double Primitive::unbox_double(java_handle_t *h)
459 java_lang_Double d(h);
460 return d.get_value();
465 // Legacy C interface.
469 classinfo* Primitive_get_class_by_name(utf *name) { return Primitive::get_class_by_name(name); }
470 classinfo* Primitive_get_class_by_type(int type) { return Primitive::get_class_by_type(type); }
471 classinfo* Primitive_get_class_by_char(char ch) { return Primitive::get_class_by_char(ch); }
472 classinfo* Primitive_get_arrayclass_by_name(utf *name) { return Primitive::get_arrayclass_by_name(name); }
473 classinfo* Primitive_get_arrayclass_by_type(int type) { return Primitive::get_arrayclass_by_type(type); }
474 int Primitive_get_type_by_wrapperclass(classinfo *c) { return Primitive::get_type_by_wrapperclass(c); }
475 java_handle_t* Primitive_box(int type, imm_union value) { return Primitive::box(type, value); }
476 imm_union Primitive_unbox(java_handle_t *h) { return Primitive::unbox(h); }
481 * These are local overrides for various environment variables in Emacs.
482 * Please do not remove this and leave it at the end of the file, where
483 * Emacs will automagically detect them.
484 * ---------------------------------------------------------------------
487 * indent-tabs-mode: t
491 * vim:noexpandtab:sw=4:ts=4: