1 /*
2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciArray.hpp"
26 #include "ci/ciArrayKlass.hpp"
27 #include "ci/ciConstant.hpp"
28 #include "ci/ciKlass.hpp"
29 #include "ci/ciUtilities.inline.hpp"
30 #include "oops/objArrayOop.inline.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "oops/typeArrayOop.inline.hpp"
33 #include "utilities/powerOfTwo.hpp"
34
35 // ciArray
36 //
37 // This class represents an arrayOop in the HotSpot virtual
38 // machine.
39 static BasicType fixup_element_type(BasicType bt) {
40 if (is_reference_type(bt)) return T_OBJECT;
41 if (bt == T_BOOLEAN) return T_BYTE;
42 return bt;
43 }
44
45 ciConstant ciArray::element_value_impl(BasicType elembt,
46 arrayOop ary,
47 int index) {
48 if (ary == nullptr)
49 return ciConstant();
50 assert(ary->is_array(), "");
51 if (index < 0 || index >= ary->length())
52 return ciConstant();
53 ArrayKlass* ak = (ArrayKlass*) ary->klass();
54 BasicType abt = ak->element_type();
55 if (fixup_element_type(elembt) !=
56 fixup_element_type(abt))
57 return ciConstant();
58 switch (elembt) {
59 case T_ARRAY:
60 case T_OBJECT:
61 {
62 assert(ary->is_objArray(), "");
63 objArrayOop objary = (objArrayOop) ary;
64 oop elem = objary->obj_at(index);
65 return ciConstant(elembt, CURRENT_ENV->get_object(elem));
66 }
67 default:
68 break;
69 }
70 assert(ary->is_typeArray(), "");
71 typeArrayOop tary = (typeArrayOop) ary;
72 jint value = 0;
73 switch (elembt) {
74 case T_LONG: return ciConstant(tary->long_at(index));
75 case T_FLOAT: return ciConstant(tary->float_at(index));
76 case T_DOUBLE: return ciConstant(tary->double_at(index));
77 default: return ciConstant();
78 case T_BYTE: value = tary->byte_at(index); break;
79 case T_BOOLEAN: value = tary->byte_at(index) & 1; break;
80 case T_SHORT: value = tary->short_at(index); break;
81 case T_CHAR: value = tary->char_at(index); break;
82 case T_INT: value = tary->int_at(index); break;
83 }
84 return ciConstant(elembt, value);
85 }
102 return value;
103 }
104
105 // ------------------------------------------------------------------
106 // ciArray::element_value_by_offset
107 //
108 // Current value of an element at the specified offset.
109 // Returns T_ILLEGAL if there is no element at the given offset.
110 ciConstant ciArray::element_value_by_offset(intptr_t element_offset) {
111 BasicType elembt = element_basic_type();
112 intptr_t shift = exact_log2(type2aelembytes(elembt));
113 intptr_t header = arrayOopDesc::base_offset_in_bytes(elembt);
114 intptr_t index = (element_offset - header) >> shift;
115 intptr_t offset = header + ((intptr_t)index << shift);
116 if (offset != element_offset || index != (jint)index || index < 0 || index >= length()) {
117 return ciConstant();
118 }
119 return element_value((jint) index);
120 }
121
122 // ------------------------------------------------------------------
123 // ciArray::print_impl
124 //
125 // Implementation of the print method.
126 void ciArray::print_impl(outputStream* st) {
127 st->print(" length=%d type=", length());
128 klass()->print(st);
129 }
|
1 /*
2 * Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciArray.hpp"
26 #include "ci/ciArrayKlass.hpp"
27 #include "ci/ciConstant.hpp"
28 #include "ci/ciKlass.hpp"
29 #include "ci/ciUtilities.inline.hpp"
30 #include "oops/arrayOop.inline.hpp"
31 #include "oops/flatArrayKlass.hpp"
32 #include "oops/layoutKind.hpp"
33 #include "oops/objArrayOop.inline.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "oops/oopCast.inline.hpp"
36 #include "oops/typeArrayOop.inline.hpp"
37 #include "utilities/powerOfTwo.hpp"
38
39 // ciArray
40 //
41 // This class represents an arrayOop in the HotSpot virtual
42 // machine.
43
44 arrayOop ciArray::get_arrayOop() const {
45 return oop_cast<arrayOop>(get_oop());
46 }
47
48 static BasicType fixup_element_type(BasicType bt) {
49 if (bt == T_FLAT_ELEMENT) return T_OBJECT;
50 if (is_reference_type(bt)) return T_OBJECT;
51 if (bt == T_BOOLEAN) return T_BYTE;
52 return bt;
53 }
54
55 ciConstant ciArray::element_value_impl(BasicType elembt,
56 arrayOop ary,
57 int index) {
58 if (ary == nullptr)
59 return ciConstant();
60 assert(ary->is_array(), "");
61 if (index < 0 || index >= ary->length())
62 return ciConstant();
63 ArrayKlass* ak = (ArrayKlass*) ary->klass();
64 BasicType abt = ak->element_type();
65 if (fixup_element_type(elembt) !=
66 fixup_element_type(abt))
67 return ciConstant();
68 switch (elembt) {
69 case T_ARRAY:
70 case T_OBJECT:
71 {
72 if (ary->is_refArray()) {
73 refArrayOop refary = oop_cast<refArrayOop>(ary);
74 oop elem = refary->obj_at(index);
75 return ciConstant(elembt, CURRENT_ENV->get_object(elem));
76 } else {
77 assert(ary->is_flatArray(), "");
78 flatArrayOop flatary = oop_cast<flatArrayOop>(ary);
79 assert(CompilerThread::current()->thread_state() == _thread_in_vm, "");
80 JavaThread* THREAD = CompilerThread::current();
81 oop elem = flatary->obj_at(index, THREAD);
82 if (HAS_PENDING_EXCEPTION) {
83 CLEAR_PENDING_EXCEPTION;
84 return ciConstant();
85 }
86 return ciConstant(elembt, CURRENT_ENV->get_object(elem));
87 }
88 }
89 default:
90 break;
91 }
92 assert(ary->is_typeArray(), "");
93 typeArrayOop tary = (typeArrayOop) ary;
94 jint value = 0;
95 switch (elembt) {
96 case T_LONG: return ciConstant(tary->long_at(index));
97 case T_FLOAT: return ciConstant(tary->float_at(index));
98 case T_DOUBLE: return ciConstant(tary->double_at(index));
99 default: return ciConstant();
100 case T_BYTE: value = tary->byte_at(index); break;
101 case T_BOOLEAN: value = tary->byte_at(index) & 1; break;
102 case T_SHORT: value = tary->short_at(index); break;
103 case T_CHAR: value = tary->char_at(index); break;
104 case T_INT: value = tary->int_at(index); break;
105 }
106 return ciConstant(elembt, value);
107 }
124 return value;
125 }
126
127 // ------------------------------------------------------------------
128 // ciArray::element_value_by_offset
129 //
130 // Current value of an element at the specified offset.
131 // Returns T_ILLEGAL if there is no element at the given offset.
132 ciConstant ciArray::element_value_by_offset(intptr_t element_offset) {
133 BasicType elembt = element_basic_type();
134 intptr_t shift = exact_log2(type2aelembytes(elembt));
135 intptr_t header = arrayOopDesc::base_offset_in_bytes(elembt);
136 intptr_t index = (element_offset - header) >> shift;
137 intptr_t offset = header + ((intptr_t)index << shift);
138 if (offset != element_offset || index != (jint)index || index < 0 || index >= length()) {
139 return ciConstant();
140 }
141 return element_value((jint) index);
142 }
143
144 bool ciArray::is_null_free() {
145 VM_ENTRY_MARK;
146 return get_arrayOop()->is_null_free_array();
147 }
148
149 bool ciArray::is_atomic() {
150 VM_ENTRY_MARK;
151 arrayOop oop = get_arrayOop();
152 return !oop->is_flatArray() || LayoutKindHelper::is_atomic_flat(FlatArrayKlass::cast(oop->klass())->layout_kind());
153 }
154
155 // ------------------------------------------------------------------
156 // ciArray::print_impl
157 //
158 // Implementation of the print method.
159 void ciArray::print_impl(outputStream* st) {
160 st->print(" length=%d type=", length());
161 klass()->print(st);
162 }
|