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