From 9380e20e34999490fda5769cb902e61235233d29 Mon Sep 17 00:00:00 2001 From: twisti Date: Thu, 16 Aug 2007 17:52:48 +0000 Subject: [PATCH] * src/vm/primitive.c (primitive_type_get_by_wrapperclass): New function. (primitive_unbox): Changed signature. * src/vm/primitive.h: Likewise. * src/vm/vm.c (vm_array_from_objectarray): Rewritten to use primitive-unbox function. --- src/vm/primitive.c | 32 +++++++++++- src/vm/primitive.h | 5 +- src/vm/vm.c | 123 ++++++++++++++++++++++++--------------------- 3 files changed, 100 insertions(+), 60 deletions(-) diff --git a/src/vm/primitive.c b/src/vm/primitive.c index 93eac1d73..81a0d34fc 100644 --- a/src/vm/primitive.c +++ b/src/vm/primitive.c @@ -168,6 +168,28 @@ classinfo *primitive_arrayclass_get_by_type(int type) } +/* primitive_type_get_by_wrapperclass ****************************************** + + Returns the primitive type of the given wrapper-class. + +*******************************************************************************/ + +int primitive_type_get_by_wrapperclass(classinfo *c) +{ + int i; + + /* Search primitive table. */ + + for (i = 0; i < PRIMITIVETYPE_COUNT; i++) + if (primitivetype_table[i].class_wrap == c) + return i; + + /* Invalid primitive wrapper-class. */ + + return -1; +} + + /* primitive_box *************************************************************** Box a primitive of the given type. @@ -217,9 +239,15 @@ java_handle_t *primitive_box(int type, imm_union value) *******************************************************************************/ -imm_union primitive_unbox(int type, java_handle_t *o) +imm_union primitive_unbox(java_handle_t *o) { - imm_union value; + classinfo *c; + int type; + imm_union value; + + c = o->vftbl->class; + + type = primitive_type_get_by_wrapperclass(c); switch (type) { case PRIMITIVETYPE_BOOLEAN: diff --git a/src/vm/primitive.h b/src/vm/primitive.h index 5695f8fdd..8b506cb2c 100644 --- a/src/vm/primitive.h +++ b/src/vm/primitive.h @@ -110,11 +110,14 @@ bool primitive_init(void); classinfo *primitive_class_get_by_name(utf *name); classinfo *primitive_class_get_by_type(int type); classinfo *primitive_class_get_by_char(char ch); + classinfo *primitive_arrayclass_get_by_name(utf *name); classinfo *primitive_arrayclass_get_by_type(int type); +int primitive_type_get_by_wrapperclass(classinfo *c); + java_handle_t *primitive_box(int type, imm_union value); -imm_union primitive_unbox(int type, java_handle_t *o); +imm_union primitive_unbox(java_handle_t *o); java_handle_t *primitive_box_boolean(int32_t value); java_handle_t *primitive_box_byte(int32_t value); diff --git a/src/vm/vm.c b/src/vm/vm.c index 6269db5a5..c0b2ea7db 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - $Id: vm.c 8318 2007-08-16 10:05:34Z michi $ + $Id: vm.c 8327 2007-08-16 17:52:48Z twisti $ */ @@ -2716,6 +2716,7 @@ uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o, uint64_t *array; java_handle_t *param; classinfo *c; + int type; int32_t i; int32_t j; imm_union value; @@ -2753,54 +2754,69 @@ uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o, /* convert the value according to its declared type */ - c = param->vftbl->class; + c = param->vftbl->class; + type = primitive_type_get_by_wrapperclass(c); switch (td->decltype) { case PRIMITIVETYPE_BOOLEAN: - if (c == class_java_lang_Boolean) - LLNI_field_get_val((java_lang_Boolean *) param, value, value.i); - else + switch (type) { + case PRIMITIVETYPE_BOOLEAN: + /* This type is OK. */ + break; + default: goto illegal_arg; + } break; case PRIMITIVETYPE_BYTE: - if (c == class_java_lang_Byte) - LLNI_field_get_val((java_lang_Byte *) param, value, value.i); - else + switch (type) { + case PRIMITIVETYPE_BYTE: + /* This type is OK. */ + break; + default: goto illegal_arg; + } break; case PRIMITIVETYPE_CHAR: - if (c == class_java_lang_Character) - LLNI_field_get_val((java_lang_Character *) param, value, value.i); - else + switch (type) { + case PRIMITIVETYPE_CHAR: + /* This type is OK. */ + break; + default: goto illegal_arg; + } break; case PRIMITIVETYPE_SHORT: - if (c == class_java_lang_Short) - LLNI_field_get_val((java_lang_Short *) param, value, value.i); - else if (c == class_java_lang_Byte) - LLNI_field_get_val((java_lang_Byte *) param, value, value.i); - else + switch (type) { + case PRIMITIVETYPE_BYTE: + case PRIMITIVETYPE_SHORT: + /* These types are OK. */ + break; + default: goto illegal_arg; + } break; case PRIMITIVETYPE_INT: - if (c == class_java_lang_Integer) - LLNI_field_get_val((java_lang_Integer *) param, value, value.i); - else if (c == class_java_lang_Short) - LLNI_field_get_val((java_lang_Short *) param, value, value.i); - else if (c == class_java_lang_Byte) - LLNI_field_get_val((java_lang_Byte *) param, value, value.i); - else + switch (type) { + case PRIMITIVETYPE_BYTE: + case PRIMITIVETYPE_SHORT: + case PRIMITIVETYPE_INT: + /* These types are OK. */ + break; + default: goto illegal_arg; + } break; default: - goto illegal_arg; + vm_abort("vm_array_from_objectarray: invalid type %d", + td->decltype); } + value = primitive_unbox(param); vm_array_store_int(array, pd, value.i); break; @@ -2808,28 +2824,24 @@ uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o, if (param == NULL) goto illegal_arg; - /* convert the value according to its declared type */ + c = param->vftbl->class; + type = primitive_type_get_by_class(c); - c = param->vftbl->class; + assert(td->decltype == PRIMITIVETYPE_LONG); - switch (td->decltype) { + switch (type) { + case PRIMITIVETYPE_BYTE: + case PRIMITIVETYPE_SHORT: + case PRIMITIVETYPE_INT: case PRIMITIVETYPE_LONG: - if (c == class_java_lang_Long) - LLNI_field_get_val((java_lang_Long *) param, value, value.l); - else if (c == class_java_lang_Integer) - value.l = (int64_t) LLNI_field_direct(((java_lang_Integer *) param), value); - else if (c == class_java_lang_Short) - value.l = (int64_t) LLNI_field_direct(((java_lang_Short *) param), value); - else if (c == class_java_lang_Byte) - value.l = (int64_t) LLNI_field_direct(((java_lang_Byte *) param), value); - else - goto illegal_arg; + /* These types are OK. */ break; - default: goto illegal_arg; } + log_println("TRACE 2"); + value = primitive_unbox(param); vm_array_store_lng(array, pd, value.l); break; @@ -2837,22 +2849,21 @@ uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o, if (param == NULL) goto illegal_arg; - /* convert the value according to its declared type */ + c = param->vftbl->class; + type = primitive_type_get_by_class(c); - c = param->vftbl->class; + assert(td->decltype == PRIMITIVETYPE_FLOAT); - switch (td->decltype) { + switch (type) { case PRIMITIVETYPE_FLOAT: - if (c == class_java_lang_Float) - LLNI_field_get_val((java_lang_Float *) param, value, value.f); - else - goto illegal_arg; + /* This type is OK. */ break; - default: goto illegal_arg; } + log_println("TRACE 3"); + value = primitive_unbox(param); vm_array_store_flt(array, pd, value.l); break; @@ -2860,24 +2871,22 @@ uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o, if (param == NULL) goto illegal_arg; - /* convert the value according to its declared type */ + c = param->vftbl->class; + type = primitive_type_get_by_class(c); - c = param->vftbl->class; + assert(td->decltype == PRIMITIVETYPE_DOUBLE); - switch (td->decltype) { + switch (type) { + case PRIMITIVETYPE_FLOAT: case PRIMITIVETYPE_DOUBLE: - if (c == class_java_lang_Double) - LLNI_field_get_val((java_lang_Double *) param, value, value.d); - else if (c == class_java_lang_Float) - LLNI_field_get_val((java_lang_Float *) param, value, value.f); - else - goto illegal_arg; + /* These types are OK. */ break; - default: goto illegal_arg; } + log_println("TRACE 4"); + value = primitive_unbox(param); vm_array_store_dbl(array, pd, value.l); break; @@ -2900,7 +2909,7 @@ uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o, break; default: - goto illegal_arg; + vm_abort("vm_array_from_objectarray: invalid type %d", td->type); } } -- 2.25.1