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/ciCallSite.hpp"
26 #include "ci/ciFlatArray.hpp"
27 #include "ci/ciFlatArrayKlass.hpp"
28 #include "ci/ciInlineKlass.hpp"
29 #include "ci/ciInstance.hpp"
30 #include "ci/ciInstanceKlass.hpp"
31 #include "ci/ciMemberName.hpp"
32 #include "ci/ciMethod.hpp"
33 #include "ci/ciMethodData.hpp"
34 #include "ci/ciMethodHandle.hpp"
35 #include "ci/ciMethodType.hpp"
36 #include "ci/ciNullObject.hpp"
37 #include "ci/ciObjArray.hpp"
38 #include "ci/ciObjArrayKlass.hpp"
39 #include "ci/ciObject.hpp"
40 #include "ci/ciObjectFactory.hpp"
41 #include "ci/ciRefArrayKlass.hpp"
42 #include "ci/ciReplay.hpp"
43 #include "ci/ciSymbol.hpp"
44 #include "ci/ciSymbols.hpp"
45 #include "ci/ciTypeArray.hpp"
46 #include "ci/ciTypeArrayKlass.hpp"
47 #include "ci/ciUtilities.inline.hpp"
48 #include "classfile/javaClasses.inline.hpp"
49 #include "classfile/vmClasses.hpp"
50 #include "compiler/compiler_globals.hpp"
51 #include "compiler/compileTask.hpp"
52 #include "gc/shared/collectedHeap.inline.hpp"
53 #include "memory/allocation.inline.hpp"
54 #include "memory/universe.hpp"
55 #include "oops/instanceKlass.hpp"
56 #include "oops/oop.inline.hpp"
57 #include "oops/trainingData.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/signature.hpp"
60 #include "utilities/macros.hpp"
61
62 // ciObjectFactory
63 //
64 // This class handles requests for the creation of new instances
65 // of ciObject and its subclasses. It contains a caching mechanism
66 // which ensures that for each oop, at most one ciObject is created.
67 // This invariant allows more efficient implementation of ciObject.
68 //
69 // Implementation note: the oop->ciObject mapping is represented as
70 // a table stored in an array. Even though objects are moved
71 // by the garbage collector, the compactor preserves their relative
72 // order; address comparison of oops (in perm space) is safe so long
73 // as we prohibit GC during our comparisons. We currently use binary
74 // search to find the oop in the table, and inserting a new oop
75 // into the table may be costly. If this cost ends up being
76 // problematic the underlying data structure can be switched to some
77 // sort of balanced binary tree.
78
79 GrowableArray<ciMetadata*>* ciObjectFactory::_shared_ci_metadata = nullptr;
80 ciSymbol* ciObjectFactory::_shared_ci_symbols[vmSymbols::number_of_symbols()];
81 int ciObjectFactory::_shared_ident_limit = 0;
82 volatile bool ciObjectFactory::_initialized = false;
83
84
85 // ------------------------------------------------------------------
86 // ciObjectFactory::ciObjectFactory
87 ciObjectFactory::ciObjectFactory(Arena* arena,
88 int expected_size)
89 : _arena(arena),
90 _ci_metadata(arena, expected_size, 0, nullptr),
91 _cached_init_state(arena, _shared_ident_limit, 0, (u1)0),
92 _unloaded_methods(arena, 4, 0, nullptr),
93 _unloaded_klasses(arena, 8, 0, nullptr),
94 _unloaded_instances(arena, 4, 0, nullptr),
95 _return_addresses(arena, 8, 0, nullptr),
96 _symbols(arena, 100, 0, nullptr),
97 _next_ident(_shared_ident_limit),
98 _non_perm_count(0) {
99 for (int i = 0; i < NON_PERM_BUCKETS; i++) {
100 _non_perm_bucket[i] = nullptr;
101 }
102
103 // If the shared ci objects exist append them to this factory's objects
104 if (_shared_ci_metadata != nullptr) {
105 _ci_metadata.appendAll(_shared_ci_metadata);
106 // ciInstanceKlass for well-known class is shared by all
107 // compiler threads and can be updated concurrently by
108 // other compiler threads during compilation.
109 // Make local copy of class state to avoid state change
110 // during compilation.
111 int len = _ci_metadata.length();
112 for (int i = 0; i < len; i++) {
113 ciMetadata* obj = _ci_metadata.at(i);
114 if (obj->is_loaded() && obj->is_instance_klass()) {
115 ciInstanceKlass* cik = obj->as_instance_klass();
116 precond(cik->is_shared());
117 InstanceKlass::ClassState current_state = cik->_init_state;
118 InstanceKlass::ClassState state = InstanceKlass::fully_initialized;
119 if (current_state != state) {
120 GUARDED_VM_ENTRY( state = cik->get_instanceKlass()->init_state(); )
121 // Update state of shared ciInstanceKlass
122 cik->_init_state = state;
123 }
124 // Cache state for current compilation
125 _cached_init_state.at_put_grow(cik->ident(), (u1)state, 0);
126 }
127 }
128 }
129 }
130
131 // ------------------------------------------------------------------
132 // ciObjectFactory::ciObjectFactory
133 void ciObjectFactory::initialize() {
134 ASSERT_IN_VM;
135 JavaThread* thread = JavaThread::current();
136 HandleMark handle_mark(thread);
137
138 // This Arena is long lived and exists in the resource mark of the
139 // compiler thread that initializes the initial ciObjectFactory which
140 // creates the shared ciObjects that all later ciObjectFactories use.
141 Arena* arena = new (mtCompiler) Arena(mtCompiler);
142 ciEnv initial(arena);
143 ciEnv* env = ciEnv::current();
144 env->_factory->init_shared_objects();
145
146 _initialized = true;
147
148 }
149
150 void ciObjectFactory::init_shared_objects() {
151
152 _next_ident = 1; // start numbering CI objects at 1
153
154 {
155 // Create the shared symbols, but not in _shared_ci_metadata.
156 for (auto index : EnumRange<vmSymbolID>{}) {
157 Symbol* vmsym = vmSymbols::symbol_at(index);
158 assert(vmSymbols::find_sid(vmsym) == index, "1-1 mapping");
159 ciSymbol* sym = new (_arena) ciSymbol(vmsym, index);
160 init_ident_of(sym);
161 _shared_ci_symbols[vmSymbols::as_int(index)] = sym;
162 }
163 #ifdef ASSERT
164 for (auto index : EnumRange<vmSymbolID>{}) {
165 Symbol* vmsym = vmSymbols::symbol_at(index);
166 ciSymbol* sym = vm_symbol_at(index);
167 assert(sym->get_symbol() == vmsym, "oop must match");
168 }
169 assert(ciSymbols::void_class_signature()->get_symbol() == vmSymbols::void_class_signature(), "spot check");
170 #endif
171 }
172
173 for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
174 BasicType t = (BasicType)i;
175 if (type2name(t) != nullptr && t != T_FLAT_ELEMENT && !is_reference_type(t) &&
176 t != T_NARROWOOP && t != T_NARROWKLASS) {
177 ciType::_basic_types[t] = new (_arena) ciType(t);
178 init_ident_of(ciType::_basic_types[t]);
179 }
180 }
181
182 ciEnv::_null_object_instance = new (_arena) ciNullObject();
183 init_ident_of(ciEnv::_null_object_instance);
184
185 #define VM_CLASS_DEFN(name, ignore_s) \
186 if (vmClasses::name##_is_loaded()) \
187 ciEnv::_##name = get_metadata(vmClasses::name())->as_instance_klass();
188
189 VM_CLASSES_DO(VM_CLASS_DEFN)
190 #undef VM_CLASS_DEFN
191
192 for (int len = -1; len != _ci_metadata.length(); ) {
193 len = _ci_metadata.length();
194 for (int i2 = 0; i2 < len; i2++) {
195 ciMetadata* obj = _ci_metadata.at(i2);
196 assert (obj->is_metadata(), "what else would it be?");
197 if (obj->is_loaded() && obj->is_instance_klass()) {
198 obj->as_instance_klass()->compute_nonstatic_fields();
199 obj->as_instance_klass()->transitive_interfaces();
200 }
201 }
202 }
203
204 ciEnv::_unloaded_cisymbol = ciObjectFactory::get_symbol(vmSymbols::dummy_symbol());
205 // Create dummy InstanceKlass and ObjArrayKlass object and assign them idents
206 ciEnv::_unloaded_ciinstance_klass = new (_arena) ciInstanceKlass(ciEnv::_unloaded_cisymbol, nullptr);
207 init_ident_of(ciEnv::_unloaded_ciinstance_klass);
208 ciEnv::_unloaded_ciobjarrayklass = new (_arena) ciObjArrayKlass(ciEnv::_unloaded_cisymbol, ciEnv::_unloaded_ciinstance_klass, 1);
209 init_ident_of(ciEnv::_unloaded_ciobjarrayklass);
210 assert(ciEnv::_unloaded_ciobjarrayklass->is_obj_array_klass(), "just checking");
211
212 get_metadata(Universe::boolArrayKlass());
213 get_metadata(Universe::charArrayKlass());
214 get_metadata(Universe::floatArrayKlass());
215 get_metadata(Universe::doubleArrayKlass());
216 get_metadata(Universe::byteArrayKlass());
217 get_metadata(Universe::shortArrayKlass());
218 get_metadata(Universe::intArrayKlass());
219 get_metadata(Universe::longArrayKlass());
220
221 assert(_non_perm_count == 0, "no shared non-perm objects");
222
223 // The shared_ident_limit is the first ident number that will
224 // be used for non-shared objects. That is, numbers less than
225 // this limit are permanently assigned to shared CI objects,
226 // while the higher numbers are recycled afresh by each new ciEnv.
227
228 _shared_ident_limit = _next_ident;
229 _shared_ci_metadata = &_ci_metadata;
230 }
231
232
233 ciSymbol* ciObjectFactory::get_symbol(Symbol* key) {
234 vmSymbolID sid = vmSymbols::find_sid(key);
235 if (sid != vmSymbolID::NO_SID) {
236 // do not pollute the main cache with it
237 return vm_symbol_at(sid);
238 }
239
240 assert(vmSymbols::find_sid(key) == vmSymbolID::NO_SID, "");
241 ciSymbol* s = new (arena()) ciSymbol(key, vmSymbolID::NO_SID);
242 _symbols.push(s);
243 return s;
244 }
245
246 // Decrement the refcount when done on symbols referenced by this compilation.
247 void ciObjectFactory::remove_symbols() {
248 for (int i = 0; i < _symbols.length(); i++) {
249 ciSymbol* s = _symbols.at(i);
250 s->get_symbol()->decrement_refcount();
251 }
252 // Since _symbols is resource allocated we're not allowed to delete it
253 // but it'll go away just the same.
254 }
255
256 // ------------------------------------------------------------------
257 // ciObjectFactory::get
258 //
259 // Get the ciObject corresponding to some oop. If the ciObject has
260 // already been created, it is returned. Otherwise, a new ciObject
261 // is created.
262 ciObject* ciObjectFactory::get(oop key) {
263 ASSERT_IN_VM;
264
265 Handle keyHandle(Thread::current(), key);
266 assert(Universe::heap()->is_in(keyHandle()), "must be");
267
268 NonPermObject* &bucket = find_non_perm(keyHandle);
269 if (bucket != nullptr) {
270 return bucket->object();
271 }
272
273 // The ciObject does not yet exist. Create it and insert it
274 // into the cache.
275 ciObject* new_object = create_new_object(keyHandle());
276 assert(keyHandle() == new_object->get_oop(), "must be properly recorded");
277 init_ident_of(new_object);
278 assert(Universe::heap()->is_in(new_object->get_oop()), "must be");
279
280 // Not a perm-space object.
281 insert_non_perm(bucket, keyHandle, new_object);
282 notice_new_object(new_object);
283 return new_object;
284 }
285
286 void ciObjectFactory::notice_new_object(ciBaseObject* new_object) {
287 if (TrainingData::need_data()) {
288 ciEnv* env = ciEnv::current();
289 if (env->task() != nullptr) {
290 // Note: task will be null during init_compiler_runtime.
291 CompileTrainingData* td = env->task()->training_data();
292 if (td != nullptr) {
293 td->notice_jit_observation(env, new_object);
294 }
295 }
296 }
297 }
298
299 int ciObjectFactory::metadata_compare(Metadata* const& key, ciMetadata* const& elt) {
300 Metadata* value = elt->constant_encoding();
301 if (key < value) return -1;
302 else if (key > value) return 1;
303 else return 0;
304 }
305
306 // ------------------------------------------------------------------
307 // ciObjectFactory::cached_metadata
308 //
309 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
310 // already been created, it is returned. Otherwise, null is returned.
311 ciMetadata* ciObjectFactory::cached_metadata(Metadata* key) {
312 ASSERT_IN_VM;
313
314 bool found = false;
315 int index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
316
317 if (!found) {
318 return nullptr;
319 }
320 return _ci_metadata.at(index)->as_metadata();
321 }
322
323
324 // ------------------------------------------------------------------
325 // ciObjectFactory::get_metadata
326 //
327 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
328 // already been created, it is returned. Otherwise, a new ciMetadata
329 // is created.
330 ciMetadata* ciObjectFactory::get_metadata(Metadata* key) {
331 ASSERT_IN_VM;
332
333 if (ReplayCompiles && key->is_klass()) {
334 Klass* k = (Klass*)key;
335 if (k->is_instance_klass() && ciReplay::is_klass_unresolved((InstanceKlass*)k)) {
336 // Klass was unresolved at replay dump time. Simulate this case.
337 return ciEnv::_unloaded_ciinstance_klass;
338 }
339 }
340
341 #ifdef ASSERT
342 if (CIObjectFactoryVerify) {
343 Metadata* last = nullptr;
344 for (int j = 0; j < _ci_metadata.length(); j++) {
345 Metadata* o = _ci_metadata.at(j)->constant_encoding();
346 assert(last < o, "out of order");
347 last = o;
348 }
349 }
350 #endif // ASSERT
351 int len = _ci_metadata.length();
352 bool found = false;
353 int index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
354 #ifdef ASSERT
355 if (CIObjectFactoryVerify) {
356 for (int i = 0; i < _ci_metadata.length(); i++) {
357 if (_ci_metadata.at(i)->constant_encoding() == key) {
358 assert(index == i, " bad lookup");
359 }
360 }
361 }
362 #endif
363
364 if (!found) {
365 // The ciMetadata does not yet exist. Create it and insert it
366 // into the cache.
367 ciMetadata* new_object = create_new_metadata(key);
368 init_ident_of(new_object);
369 assert(new_object->is_metadata(), "must be");
370
371 if (len != _ci_metadata.length()) {
372 // creating the new object has recursively entered new objects
373 // into the table. We need to recompute our index.
374 index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
375 }
376 assert(!found, "no double insert");
377 _ci_metadata.insert_before(index, new_object);
378 notice_new_object(new_object);
379 return new_object;
380 }
381 return _ci_metadata.at(index)->as_metadata();
382 }
383
384 // ------------------------------------------------------------------
385 // ciObjectFactory::create_new_object
386 //
387 // Create a new ciObject from an oop.
388 //
389 // Implementation note: this functionality could be virtual behavior
390 // of the oop itself. For now, we explicitly marshal the object.
391 ciObject* ciObjectFactory::create_new_object(oop o) {
392 EXCEPTION_CONTEXT;
393
394 if (o->is_instance()) {
395 instanceHandle h_i(THREAD, (instanceOop)o);
396 if (java_lang_invoke_CallSite::is_instance(o))
397 return new (arena()) ciCallSite(h_i);
398 else if (java_lang_invoke_MemberName::is_instance(o))
399 return new (arena()) ciMemberName(h_i);
400 else if (java_lang_invoke_MethodHandle::is_instance(o))
401 return new (arena()) ciMethodHandle(h_i);
402 else if (java_lang_invoke_MethodType::is_instance(o))
403 return new (arena()) ciMethodType(h_i);
404 else
405 return new (arena()) ciInstance(h_i);
406 } else if (o->is_refArray()) {
407 objArrayHandle h_oa(THREAD, (objArrayOop)o);
408 return new (arena()) ciObjArray(h_oa);
409 } else if (o->is_typeArray()) {
410 typeArrayHandle h_ta(THREAD, (typeArrayOop)o);
411 return new (arena()) ciTypeArray(h_ta);
412 } else if (o->is_flatArray()) {
413 flatArrayHandle h_ta(THREAD, (flatArrayOop)o);
414 return new (arena()) ciFlatArray(h_ta);
415 }
416
417 // The oop is of some type not supported by the compiler interface.
418 ShouldNotReachHere();
419 return nullptr;
420 }
421
422 // ------------------------------------------------------------------
423 // ciObjectFactory::create_new_metadata
424 //
425 // Create a new ciMetadata from a Metadata*.
426 //
427 // Implementation note: in order to keep Metadata live, an auxiliary ciObject
428 // is used, which points to it's holder.
429 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) {
430 EXCEPTION_CONTEXT;
431
432 if (o->is_klass()) {
433 Klass* k = (Klass*)o;
434 if (k->is_inline_klass()) {
435 return new (arena()) ciInlineKlass(k);
436 } else if (k->is_instance_klass()) {
437 assert(!ReplayCompiles || ciReplay::no_replay_state() || !ciReplay::is_klass_unresolved((InstanceKlass*)k), "must be whitelisted for replay compilation");
438 return new (arena()) ciInstanceKlass(k);
439 } else if (k->is_objArray_klass()) {
440 if (k->is_flatArray_klass()) {
441 return new (arena()) ciFlatArrayKlass(k);
442 } else if (k->is_refArray_klass()) {
443 return new (arena()) ciRefArrayKlass(k);
444 } else {
445 return new (arena()) ciObjArrayKlass(k);
446 }
447 } else if (k->is_typeArray_klass()) {
448 return new (arena()) ciTypeArrayKlass(k);
449 }
450 } else if (o->is_method()) {
451 methodHandle h_m(THREAD, (Method*)o);
452 ciEnv *env = CURRENT_THREAD_ENV;
453 ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder());
454 return new (arena()) ciMethod(h_m, holder);
455 } else if (o->is_methodData()) {
456 // Hold methodHandle alive - might not be necessary ???
457 methodHandle h_m(THREAD, ((MethodData*)o)->method());
458 return new (arena()) ciMethodData((MethodData*)o);
459 }
460
461 // The Metadata* is of some type not supported by the compiler interface.
462 ShouldNotReachHere();
463 return nullptr;
464 }
465
466 //------------------------------------------------------------------
467 // ciObjectFactory::get_unloaded_method
468 //
469 // Get the ciMethod representing an unloaded/unfound method.
470 //
471 // Implementation note: unloaded methods are currently stored in
472 // an unordered array, requiring a linear-time lookup for each
473 // unloaded method. This may need to change.
474 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder,
475 ciSymbol* name,
476 ciSymbol* signature,
477 ciInstanceKlass* accessor) {
478 assert(accessor != nullptr, "need origin of access");
479 ciSignature* that = nullptr;
480 for (int i = 0; i < _unloaded_methods.length(); i++) {
481 ciMethod* entry = _unloaded_methods.at(i);
482 if (entry->holder()->equals(holder) &&
483 entry->name()->equals(name) &&
484 entry->signature()->as_symbol()->equals(signature)) {
485 // Short-circuit slow resolve.
486 if (entry->signature()->accessing_klass() == accessor) {
487 // We've found a match.
488 return entry;
489 } else {
490 // Lazily create ciSignature
491 if (that == nullptr) that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature);
492 if (entry->signature()->equals(that)) {
493 // We've found a match.
494 return entry;
495 }
496 }
497 }
498 }
499
500 // This is a new unloaded method. Create it and stick it in
501 // the cache.
502 ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor);
503
504 init_ident_of(new_method);
505 _unloaded_methods.append(new_method);
506
507 return new_method;
508 }
509
510 //------------------------------------------------------------------
511 // ciObjectFactory::get_unloaded_klass
512 //
513 // Get a ciKlass representing an unloaded klass.
514 //
515 // Implementation note: unloaded klasses are currently stored in
516 // an unordered array, requiring a linear-time lookup for each
517 // unloaded klass. This may need to change.
518 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass,
519 ciSymbol* name,
520 bool create_if_not_found) {
521 EXCEPTION_CONTEXT;
522 oop loader = nullptr;
523 oop domain = nullptr;
524 if (accessing_klass != nullptr) {
525 loader = accessing_klass->loader();
526 }
527 for (int i = 0; i < _unloaded_klasses.length(); i++) {
528 ciKlass* entry = _unloaded_klasses.at(i);
529 if (entry->name()->equals(name) &&
530 entry->loader() == loader) {
531 // We've found a match.
532 return entry;
533 }
534 }
535
536 if (!create_if_not_found)
537 return nullptr;
538
539 // This is a new unloaded klass. Create it and stick it in
540 // the cache.
541 ciKlass* new_klass = nullptr;
542
543 // Two cases: this is an unloaded ObjArrayKlass or an
544 // unloaded InstanceKlass. Deal with both.
545 if (name->char_at(0) == JVM_SIGNATURE_ARRAY) {
546 // Decompose the name.'
547 SignatureStream ss(name->get_symbol(), false);
548 int dimension = ss.skip_array_prefix(); // skip all '['s
549 BasicType element_type = ss.type();
550 assert(element_type != T_ARRAY, "unsuccessful decomposition");
551 ciKlass* element_klass = nullptr;
552 if (element_type == T_OBJECT) {
553 ciEnv *env = CURRENT_THREAD_ENV;
554 ciSymbol* ci_name = env->get_symbol(ss.as_symbol());
555 element_klass =
556 env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass();
557 } else {
558 assert(dimension > 1, "one dimensional type arrays are always loaded.");
559
560 // The type array itself takes care of one of the dimensions.
561 dimension--;
562
563 // The element klass is a TypeArrayKlass.
564 element_klass = ciTypeArrayKlass::make(element_type);
565 }
566 new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension);
567 } else {
568 jobject loader_handle = nullptr;
569 if (accessing_klass != nullptr) {
570 loader_handle = accessing_klass->loader_handle();
571 }
572 new_klass = new (arena()) ciInstanceKlass(name, loader_handle);
573 }
574 init_ident_of(new_klass);
575 _unloaded_klasses.append(new_klass);
576
577 return new_klass;
578 }
579
580
581 //------------------------------------------------------------------
582 // ciObjectFactory::get_unloaded_instance
583 //
584 // Get a ciInstance representing an as-yet undetermined instance of a given class.
585 //
586 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) {
587 for (int i = 0; i < _unloaded_instances.length(); i++) {
588 ciInstance* entry = _unloaded_instances.at(i);
589 if (entry->klass()->equals(instance_klass)) {
590 // We've found a match.
591 return entry;
592 }
593 }
594
595 // This is a new unloaded instance. Create it and stick it in
596 // the cache.
597 ciInstance* new_instance = new (arena()) ciInstance(instance_klass);
598
599 init_ident_of(new_instance);
600 _unloaded_instances.append(new_instance);
601
602 // make sure it looks the way we want:
603 assert(!new_instance->is_loaded(), "");
604 assert(new_instance->klass() == instance_klass, "");
605
606 return new_instance;
607 }
608
609
610 //------------------------------------------------------------------
611 // ciObjectFactory::get_unloaded_klass_mirror
612 //
613 // Get a ciInstance representing an unresolved klass mirror.
614 //
615 // Currently, this ignores the parameters and returns a unique unloaded instance.
616 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass* type) {
617 assert(ciEnv::_Class_klass != nullptr, "");
618 return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass());
619 }
620
621 //------------------------------------------------------------------
622 // ciObjectFactory::get_unloaded_method_handle_constant
623 //
624 // Get a ciInstance representing an unresolved method handle constant.
625 //
626 // Currently, this ignores the parameters and returns a unique unloaded instance.
627 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass* holder,
628 ciSymbol* name,
629 ciSymbol* signature,
630 int ref_kind) {
631 assert(ciEnv::_MethodHandle_klass != nullptr, "");
632 return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass());
633 }
634
635 //------------------------------------------------------------------
636 // ciObjectFactory::get_unloaded_method_type_constant
637 //
638 // Get a ciInstance representing an unresolved method type constant.
639 //
640 // Currently, this ignores the parameters and returns a unique unloaded instance.
641 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) {
642 assert(ciEnv::_MethodType_klass != nullptr, "");
643 return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass());
644 }
645
646 ciInstance* ciObjectFactory::get_unloaded_object_constant() {
647 assert(ciEnv::_Object_klass != nullptr, "");
648 return get_unloaded_instance(ciEnv::_Object_klass->as_instance_klass());
649 }
650
651 //------------------------------------------------------------------
652 // ciObjectFactory::get_empty_methodData
653 //
654 // Get the ciMethodData representing the methodData for a method with
655 // none.
656 ciMethodData* ciObjectFactory::get_empty_methodData() {
657 ciMethodData* new_methodData = new (arena()) ciMethodData();
658 init_ident_of(new_methodData);
659 return new_methodData;
660 }
661
662 //------------------------------------------------------------------
663 // ciObjectFactory::get_return_address
664 //
665 // Get a ciReturnAddress for a specified bci.
666 ciReturnAddress* ciObjectFactory::get_return_address(int bci) {
667 for (int i = 0; i < _return_addresses.length(); i++) {
668 ciReturnAddress* entry = _return_addresses.at(i);
669 if (entry->bci() == bci) {
670 // We've found a match.
671 return entry;
672 }
673 }
674
675 ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci);
676 init_ident_of(new_ret_addr);
677 _return_addresses.append(new_ret_addr);
678 return new_ret_addr;
679 }
680
681 ciWrapper* ciObjectFactory::make_early_larval_wrapper(ciType* type) {
682 ciWrapper* wrapper = new (arena()) ciWrapper(type, ciWrapper::EarlyLarval);
683 init_ident_of(wrapper);
684 return wrapper;
685 }
686
687 ciWrapper* ciObjectFactory::make_null_free_wrapper(ciType* type) {
688 ciWrapper* wrapper = new (arena()) ciWrapper(type, ciWrapper::NullFree);
689 init_ident_of(wrapper);
690 return wrapper;
691 }
692
693 // ------------------------------------------------------------------
694 // ciObjectFactory::init_ident_of
695 void ciObjectFactory::init_ident_of(ciBaseObject* obj) {
696 obj->set_ident(_next_ident++);
697 }
698
699 static ciObjectFactory::NonPermObject* emptyBucket = nullptr;
700
701 // ------------------------------------------------------------------
702 // ciObjectFactory::find_non_perm
703 //
704 // Use a small hash table, hashed on the klass of the key.
705 // If there is no entry in the cache corresponding to this oop, return
706 // the null tail of the bucket into which the oop should be inserted.
707 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(Handle keyHandle) {
708 assert(Universe::heap()->is_in(keyHandle()), "must be");
709 ciMetadata* klass = get_metadata(keyHandle->klass()); // This may safepoint!
710 NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
711 for (NonPermObject* p; (p = (*bp)) != nullptr; bp = &p->next()) {
712 if (is_equal(p, keyHandle())) break;
713 }
714 return (*bp);
715 }
716
717
718
719 // ------------------------------------------------------------------
720 // Code for for NonPermObject
721 //
722 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) {
723 assert(ciObjectFactory::is_initialized(), "");
724 _object = object;
725 _next = bucket;
726 bucket = this;
727 }
728
729
730
731 // ------------------------------------------------------------------
732 // ciObjectFactory::insert_non_perm
733 //
734 // Insert a ciObject into the non-perm table.
735 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, Handle keyHandle, ciObject* obj) {
736 assert(Universe::heap()->is_in_or_null(keyHandle()), "must be");
737 assert(&where != &emptyBucket, "must not try to fill empty bucket");
738 NonPermObject* p = new (arena()) NonPermObject(where, keyHandle(), obj);
739 assert(where == p && is_equal(p, keyHandle()) && p->object() == obj, "entry must match");
740 assert(find_non_perm(keyHandle) == p, "must find the same spot");
741 ++_non_perm_count;
742 }
743
744 // ------------------------------------------------------------------
745 // ciObjectFactory::vm_symbol_at
746 // Get the ciSymbol corresponding to some index in vmSymbols.
747 ciSymbol* ciObjectFactory::vm_symbol_at(vmSymbolID sid) {
748 int index = vmSymbols::as_int(sid);
749 return _shared_ci_symbols[index];
750 }
751
752 // ------------------------------------------------------------------
753 // ciObjectFactory::metadata_do
754 void ciObjectFactory::metadata_do(MetadataClosure* f) {
755 for (int j = 0; j < _ci_metadata.length(); j++) {
756 Metadata* o = _ci_metadata.at(j)->constant_encoding();
757 f->do_metadata(o);
758 }
759 }
760
761 // ------------------------------------------------------------------
762 // ciObjectFactory::print_contents_impl
763 void ciObjectFactory::print_contents_impl() {
764 int len = _ci_metadata.length();
765 tty->print_cr("ciObjectFactory (%d) meta data contents:", len);
766 for (int i = 0; i < len; i++) {
767 _ci_metadata.at(i)->print();
768 tty->cr();
769 }
770 }
771
772 // ------------------------------------------------------------------
773 // ciObjectFactory::print_contents
774 void ciObjectFactory::print_contents() {
775 print();
776 tty->cr();
777 GUARDED_VM_ENTRY(print_contents_impl();)
778 }
779
780 // ------------------------------------------------------------------
781 // ciObjectFactory::print
782 //
783 // Print debugging information about the object factory
784 void ciObjectFactory::print() {
785 tty->print("<ciObjectFactory oops=%d metadata=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>",
786 _non_perm_count, _ci_metadata.length(), _unloaded_methods.length(),
787 _unloaded_instances.length(),
788 _unloaded_klasses.length());
789 }