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