duke@435: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "ci/ciArray.hpp" vlivanov@5658: #include "ci/ciArrayKlass.hpp" vlivanov@5658: #include "ci/ciConstant.hpp" stefank@2314: #include "ci/ciKlass.hpp" stefank@2314: #include "ci/ciUtilities.hpp" vlivanov@5658: #include "oops/objArrayOop.hpp" vlivanov@5658: #include "oops/typeArrayOop.hpp" duke@435: duke@435: // ciArray duke@435: // duke@435: // This class represents an arrayOop in the HotSpot virtual duke@435: // machine. vlivanov@5658: static BasicType fixup_element_type(BasicType bt) { vlivanov@5658: if (bt == T_ARRAY) return T_OBJECT; vlivanov@5658: if (bt == T_BOOLEAN) return T_BYTE; vlivanov@5658: return bt; vlivanov@5658: } vlivanov@5658: vlivanov@5658: ciConstant ciArray::element_value_impl(BasicType elembt, vlivanov@5658: arrayOop ary, vlivanov@5658: int index) { vlivanov@5658: if (ary == NULL) vlivanov@5658: return ciConstant(); vlivanov@5658: assert(ary->is_array(), ""); vlivanov@5658: if (index < 0 || index >= ary->length()) vlivanov@5658: return ciConstant(); vlivanov@5658: ArrayKlass* ak = (ArrayKlass*) ary->klass(); vlivanov@5658: BasicType abt = ak->element_type(); vlivanov@5658: if (fixup_element_type(elembt) != vlivanov@5658: fixup_element_type(abt)) vlivanov@5658: return ciConstant(); vlivanov@5658: switch (elembt) { vlivanov@5658: case T_ARRAY: vlivanov@5658: case T_OBJECT: vlivanov@5658: { vlivanov@5658: assert(ary->is_objArray(), ""); vlivanov@5658: objArrayOop objary = (objArrayOop) ary; vlivanov@5658: oop elem = objary->obj_at(index); vlivanov@5658: ciEnv* env = CURRENT_ENV; vlivanov@5658: ciObject* box = env->get_object(elem); vlivanov@5658: return ciConstant(T_OBJECT, box); vlivanov@5658: } vlivanov@5658: } vlivanov@5658: assert(ary->is_typeArray(), ""); vlivanov@5658: typeArrayOop tary = (typeArrayOop) ary; vlivanov@5658: jint value = 0; vlivanov@5658: switch (elembt) { vlivanov@5658: case T_LONG: return ciConstant(tary->long_at(index)); vlivanov@5658: case T_FLOAT: return ciConstant(tary->float_at(index)); vlivanov@5658: case T_DOUBLE: return ciConstant(tary->double_at(index)); vlivanov@5658: default: return ciConstant(); vlivanov@5658: case T_BYTE: value = tary->byte_at(index); break; vlivanov@5658: case T_BOOLEAN: value = tary->byte_at(index) & 1; break; vlivanov@5658: case T_SHORT: value = tary->short_at(index); break; vlivanov@5658: case T_CHAR: value = tary->char_at(index); break; vlivanov@5658: case T_INT: value = tary->int_at(index); break; vlivanov@5658: } vlivanov@5658: return ciConstant(elembt, value); vlivanov@5658: } vlivanov@5658: vlivanov@5658: // ------------------------------------------------------------------ vlivanov@5658: // ciArray::element_value vlivanov@5658: // vlivanov@5658: // Current value of an element. vlivanov@5658: // Returns T_ILLEGAL if there is no element at the given index. vlivanov@5658: ciConstant ciArray::element_value(int index) { vlivanov@5658: BasicType elembt = element_basic_type(); vlivanov@5658: GUARDED_VM_ENTRY( vlivanov@5658: return element_value_impl(elembt, get_arrayOop(), index); vlivanov@5658: ) vlivanov@5658: } vlivanov@5658: vlivanov@5658: // ------------------------------------------------------------------ vlivanov@5658: // ciArray::element_value_by_offset vlivanov@5658: // vlivanov@5658: // Current value of an element at the specified offset. vlivanov@5658: // Returns T_ILLEGAL if there is no element at the given offset. vlivanov@5658: ciConstant ciArray::element_value_by_offset(intptr_t element_offset) { vlivanov@5658: BasicType elembt = element_basic_type(); vlivanov@5658: intptr_t shift = exact_log2(type2aelembytes(elembt)); vlivanov@5658: intptr_t header = arrayOopDesc::base_offset_in_bytes(elembt); vlivanov@5658: intptr_t index = (element_offset - header) >> shift; vlivanov@5658: intptr_t offset = header + ((intptr_t)index << shift); vlivanov@5658: if (offset != element_offset || index != (jint)index) vlivanov@5658: return ciConstant(); vlivanov@5658: return element_value((jint) index); vlivanov@5658: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // ciArray::print_impl duke@435: // duke@435: // Implementation of the print method. duke@435: void ciArray::print_impl(outputStream* st) { duke@435: st->print(" length=%d type=", length()); duke@435: klass()->print(st); duke@435: }