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 void ciObjectFactory::notice_object_access(ciBaseObject* new_object) {
261   if (TrainingData::need_data()) {
262     ciEnv* env = ciEnv::current();
263     if (env->task() != nullptr) {
264       // Note: task will be null during init_compiler_runtime.
265       CompileTrainingData* td = env->task()->training_data();
266       if (td != nullptr) {
267         td->notice_jit_observation(env, new_object);
268       }
269     }
270   }
271 }
272 
273 int ciObjectFactory::metadata_compare(Metadata* const& key, ciMetadata* const& elt) {
274   Metadata* value = elt->constant_encoding();
275   if (key < value)      return -1;
276   else if (key > value) return 1;
277   else                  return 0;
278 }
279 
280 // ------------------------------------------------------------------
281 // ciObjectFactory::cached_metadata
282 //
283 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
284 // already been created, it is returned. Otherwise, null is returned.
285 ciMetadata* ciObjectFactory::cached_metadata(Metadata* key) {
286   ASSERT_IN_VM;
287 
288   bool found = false;
289   int index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
290 
291   if (!found) {
292     return nullptr;
293   }
294   return _ci_metadata.at(index)->as_metadata();
295 }
296 
297 
298 // ------------------------------------------------------------------
299 // ciObjectFactory::get_metadata
300 //
301 // Get the ciMetadata corresponding to some Metadata. If the ciMetadata has
302 // already been created, it is returned. Otherwise, a new ciMetadata
303 // is created.
304 ciMetadata* ciObjectFactory::get_metadata(Metadata* key) {
305   ASSERT_IN_VM;
306 
307   if (ReplayCompiles && key->is_klass()) {
308     Klass* k = (Klass*)key;
309     if (k->is_instance_klass() && ciReplay::is_klass_unresolved((InstanceKlass*)k)) {
310       // Klass was unresolved at replay dump time. Simulate this case.
311       return ciEnv::_unloaded_ciinstance_klass;
312     }
313   }
314 
315 #ifdef ASSERT
316   if (CIObjectFactoryVerify) {
317     Metadata* last = nullptr;
318     for (int j = 0; j < _ci_metadata.length(); j++) {
319       Metadata* o = _ci_metadata.at(j)->constant_encoding();
320       assert(last < o, "out of order");
321       last = o;
322     }
323   }
324 #endif // ASSERT
325   int len = _ci_metadata.length();
326   bool found = false;
327   int index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
328 #ifdef ASSERT
329   if (CIObjectFactoryVerify) {
330     for (int i = 0; i < _ci_metadata.length(); i++) {
331       if (_ci_metadata.at(i)->constant_encoding() == key) {
332         assert(index == i, " bad lookup");
333       }
334     }
335   }
336 #endif
337 
338   if (!found) {
339     // The ciMetadata does not yet exist. Create it and insert it
340     // into the cache.
341     ciMetadata* new_object = create_new_metadata(key);
342     init_ident_of(new_object);
343     assert(new_object->is_metadata(), "must be");
344 
345     if (len != _ci_metadata.length()) {
346       // creating the new object has recursively entered new objects
347       // into the table.  We need to recompute our index.
348       index = _ci_metadata.find_sorted<Metadata*, ciObjectFactory::metadata_compare>(key, found);
349     }
350     assert(!found, "no double insert");
351     _ci_metadata.insert_before(index, new_object);
352     notice_object_access(new_object);
353     return new_object;
354   }
355   ciMetadata* metadata = _ci_metadata.at(index)->as_metadata();
356   notice_object_access(metadata);
357   return 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_objArray()) {
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   }
389 
390   // The oop is of some type not supported by the compiler interface.
391   ShouldNotReachHere();
392   return nullptr;
393 }
394 
395 // ------------------------------------------------------------------
396 // ciObjectFactory::create_new_metadata
397 //
398 // Create a new ciMetadata from a Metadata*.
399 //
400 // Implementation note: in order to keep Metadata live, an auxiliary ciObject
401 // is used, which points to it's holder.
402 ciMetadata* ciObjectFactory::create_new_metadata(Metadata* o) {
403   EXCEPTION_CONTEXT;
404 
405   if (o->is_klass()) {
406     Klass* k = (Klass*)o;
407     if (k->is_instance_klass()) {
408       assert(!ReplayCompiles || ciReplay::no_replay_state() || !ciReplay::is_klass_unresolved((InstanceKlass*)k), "must be whitelisted for replay compilation");
409       return new (arena()) ciInstanceKlass(k);
410     } else if (k->is_objArray_klass()) {
411       return new (arena()) ciObjArrayKlass(k);
412     } else if (k->is_typeArray_klass()) {
413       return new (arena()) ciTypeArrayKlass(k);
414     }
415   } else if (o->is_method()) {
416     methodHandle h_m(THREAD, (Method*)o);
417     ciEnv *env = CURRENT_THREAD_ENV;
418     ciInstanceKlass* holder = env->get_instance_klass(h_m()->method_holder());
419     return new (arena()) ciMethod(h_m, holder);
420   } else if (o->is_methodData()) {
421     // Hold methodHandle alive - might not be necessary ???
422     methodHandle h_m(THREAD, ((MethodData*)o)->method());
423     return new (arena()) ciMethodData((MethodData*)o);
424   }
425 
426   // The Metadata* is of some type not supported by the compiler interface.
427   ShouldNotReachHere();
428   return nullptr;
429 }
430 
431 //------------------------------------------------------------------
432 // ciObjectFactory::get_unloaded_method
433 //
434 // Get the ciMethod representing an unloaded/unfound method.
435 //
436 // Implementation note: unloaded methods are currently stored in
437 // an unordered array, requiring a linear-time lookup for each
438 // unloaded method.  This may need to change.
439 ciMethod* ciObjectFactory::get_unloaded_method(ciInstanceKlass* holder,
440                                                ciSymbol*        name,
441                                                ciSymbol*        signature,
442                                                ciInstanceKlass* accessor) {
443   assert(accessor != nullptr, "need origin of access");
444   ciSignature* that = nullptr;
445   for (int i = 0; i < _unloaded_methods.length(); i++) {
446     ciMethod* entry = _unloaded_methods.at(i);
447     if (entry->holder()->equals(holder) &&
448         entry->name()->equals(name) &&
449         entry->signature()->as_symbol()->equals(signature)) {
450       // Short-circuit slow resolve.
451       if (entry->signature()->accessing_klass() == accessor) {
452         // We've found a match.
453         return entry;
454       } else {
455         // Lazily create ciSignature
456         if (that == nullptr)  that = new (arena()) ciSignature(accessor, constantPoolHandle(), signature);
457         if (entry->signature()->equals(that)) {
458           // We've found a match.
459           return entry;
460         }
461       }
462     }
463   }
464 
465   // This is a new unloaded method.  Create it and stick it in
466   // the cache.
467   ciMethod* new_method = new (arena()) ciMethod(holder, name, signature, accessor);
468 
469   init_ident_of(new_method);
470   _unloaded_methods.append(new_method);
471 
472   return new_method;
473 }
474 
475 //------------------------------------------------------------------
476 // ciObjectFactory::get_unloaded_klass
477 //
478 // Get a ciKlass representing an unloaded klass.
479 //
480 // Implementation note: unloaded klasses are currently stored in
481 // an unordered array, requiring a linear-time lookup for each
482 // unloaded klass.  This may need to change.
483 ciKlass* ciObjectFactory::get_unloaded_klass(ciKlass* accessing_klass,
484                                              ciSymbol* name,
485                                              bool create_if_not_found) {
486   EXCEPTION_CONTEXT;
487   oop loader = nullptr;
488   oop domain = nullptr;
489   if (accessing_klass != nullptr) {
490     loader = accessing_klass->loader();
491   }
492   for (int i = 0; i < _unloaded_klasses.length(); i++) {
493     ciKlass* entry = _unloaded_klasses.at(i);
494     if (entry->name()->equals(name) &&
495         entry->loader() == loader) {
496       // We've found a match.
497       return entry;
498     }
499   }
500 
501   if (!create_if_not_found)
502     return nullptr;
503 
504   // This is a new unloaded klass.  Create it and stick it in
505   // the cache.
506   ciKlass* new_klass = nullptr;
507 
508   // Two cases: this is an unloaded ObjArrayKlass or an
509   // unloaded InstanceKlass.  Deal with both.
510   if (name->char_at(0) == JVM_SIGNATURE_ARRAY) {
511     // Decompose the name.'
512     SignatureStream ss(name->get_symbol(), false);
513     int dimension = ss.skip_array_prefix();  // skip all '['s
514     BasicType element_type = ss.type();
515     assert(element_type != T_ARRAY, "unsuccessful decomposition");
516     ciKlass* element_klass = nullptr;
517     if (element_type == T_OBJECT) {
518       ciEnv *env = CURRENT_THREAD_ENV;
519       ciSymbol* ci_name = env->get_symbol(ss.as_symbol());
520       element_klass =
521         env->get_klass_by_name(accessing_klass, ci_name, false)->as_instance_klass();
522     } else {
523       assert(dimension > 1, "one dimensional type arrays are always loaded.");
524 
525       // The type array itself takes care of one of the dimensions.
526       dimension--;
527 
528       // The element klass is a TypeArrayKlass.
529       element_klass = ciTypeArrayKlass::make(element_type);
530     }
531     new_klass = new (arena()) ciObjArrayKlass(name, element_klass, dimension);
532   } else {
533     jobject loader_handle = nullptr;
534     if (accessing_klass != nullptr) {
535       loader_handle = accessing_klass->loader_handle();
536     }
537     new_klass = new (arena()) ciInstanceKlass(name, loader_handle);
538   }
539   init_ident_of(new_klass);
540   _unloaded_klasses.append(new_klass);
541 
542   return new_klass;
543 }
544 
545 
546 //------------------------------------------------------------------
547 // ciObjectFactory::get_unloaded_instance
548 //
549 // Get a ciInstance representing an as-yet undetermined instance of a given class.
550 //
551 ciInstance* ciObjectFactory::get_unloaded_instance(ciInstanceKlass* instance_klass) {
552   for (int i = 0; i < _unloaded_instances.length(); i++) {
553     ciInstance* entry = _unloaded_instances.at(i);
554     if (entry->klass()->equals(instance_klass)) {
555       // We've found a match.
556       return entry;
557     }
558   }
559 
560   // This is a new unloaded instance.  Create it and stick it in
561   // the cache.
562   ciInstance* new_instance = new (arena()) ciInstance(instance_klass);
563 
564   init_ident_of(new_instance);
565   _unloaded_instances.append(new_instance);
566 
567   // make sure it looks the way we want:
568   assert(!new_instance->is_loaded(), "");
569   assert(new_instance->klass() == instance_klass, "");
570 
571   return new_instance;
572 }
573 
574 
575 //------------------------------------------------------------------
576 // ciObjectFactory::get_unloaded_klass_mirror
577 //
578 // Get a ciInstance representing an unresolved klass mirror.
579 //
580 // Currently, this ignores the parameters and returns a unique unloaded instance.
581 ciInstance* ciObjectFactory::get_unloaded_klass_mirror(ciKlass* type) {
582   assert(ciEnv::_Class_klass != nullptr, "");
583   return get_unloaded_instance(ciEnv::_Class_klass->as_instance_klass());
584 }
585 
586 //------------------------------------------------------------------
587 // ciObjectFactory::get_unloaded_method_handle_constant
588 //
589 // Get a ciInstance representing an unresolved method handle constant.
590 //
591 // Currently, this ignores the parameters and returns a unique unloaded instance.
592 ciInstance* ciObjectFactory::get_unloaded_method_handle_constant(ciKlass*  holder,
593                                                                  ciSymbol* name,
594                                                                  ciSymbol* signature,
595                                                                  int       ref_kind) {
596   assert(ciEnv::_MethodHandle_klass != nullptr, "");
597   return get_unloaded_instance(ciEnv::_MethodHandle_klass->as_instance_klass());
598 }
599 
600 //------------------------------------------------------------------
601 // ciObjectFactory::get_unloaded_method_type_constant
602 //
603 // Get a ciInstance representing an unresolved method type constant.
604 //
605 // Currently, this ignores the parameters and returns a unique unloaded instance.
606 ciInstance* ciObjectFactory::get_unloaded_method_type_constant(ciSymbol* signature) {
607   assert(ciEnv::_MethodType_klass != nullptr, "");
608   return get_unloaded_instance(ciEnv::_MethodType_klass->as_instance_klass());
609 }
610 
611 ciInstance* ciObjectFactory::get_unloaded_object_constant() {
612   assert(ciEnv::_Object_klass != nullptr, "");
613   return get_unloaded_instance(ciEnv::_Object_klass->as_instance_klass());
614 }
615 
616 //------------------------------------------------------------------
617 // ciObjectFactory::get_empty_methodData
618 //
619 // Get the ciMethodData representing the methodData for a method with
620 // none.
621 ciMethodData* ciObjectFactory::get_empty_methodData() {
622   ciMethodData* new_methodData = new (arena()) ciMethodData();
623   init_ident_of(new_methodData);
624   return new_methodData;
625 }
626 
627 //------------------------------------------------------------------
628 // ciObjectFactory::get_return_address
629 //
630 // Get a ciReturnAddress for a specified bci.
631 ciReturnAddress* ciObjectFactory::get_return_address(int bci) {
632   for (int i = 0; i < _return_addresses.length(); i++) {
633     ciReturnAddress* entry = _return_addresses.at(i);
634     if (entry->bci() == bci) {
635       // We've found a match.
636       return entry;
637     }
638   }
639 
640   ciReturnAddress* new_ret_addr = new (arena()) ciReturnAddress(bci);
641   init_ident_of(new_ret_addr);
642   _return_addresses.append(new_ret_addr);
643   return new_ret_addr;
644 }
645 
646 // ------------------------------------------------------------------
647 // ciObjectFactory::init_ident_of
648 void ciObjectFactory::init_ident_of(ciBaseObject* obj) {
649   obj->set_ident(_next_ident++);
650 }
651 
652 static ciObjectFactory::NonPermObject* emptyBucket = nullptr;
653 
654 // ------------------------------------------------------------------
655 // ciObjectFactory::find_non_perm
656 //
657 // Use a small hash table, hashed on the klass of the key.
658 // If there is no entry in the cache corresponding to this oop, return
659 // the null tail of the bucket into which the oop should be inserted.
660 ciObjectFactory::NonPermObject* &ciObjectFactory::find_non_perm(Handle keyHandle) {
661   assert(Universe::heap()->is_in(keyHandle()), "must be");
662   ciMetadata* klass = get_metadata(keyHandle->klass()); // This may safepoint!
663   NonPermObject* *bp = &_non_perm_bucket[(unsigned) klass->hash() % NON_PERM_BUCKETS];
664   for (NonPermObject* p; (p = (*bp)) != nullptr; bp = &p->next()) {
665     if (is_equal(p, keyHandle()))  break;
666   }
667   return (*bp);
668 }
669 
670 
671 
672 // ------------------------------------------------------------------
673 // Code for for NonPermObject
674 //
675 inline ciObjectFactory::NonPermObject::NonPermObject(ciObjectFactory::NonPermObject* &bucket, oop key, ciObject* object) {
676   assert(ciObjectFactory::is_initialized(), "");
677   _object = object;
678   _next = bucket;
679   bucket = this;
680 }
681 
682 
683 
684 // ------------------------------------------------------------------
685 // ciObjectFactory::insert_non_perm
686 //
687 // Insert a ciObject into the non-perm table.
688 void ciObjectFactory::insert_non_perm(ciObjectFactory::NonPermObject* &where, Handle keyHandle, ciObject* obj) {
689   assert(Universe::heap()->is_in_or_null(keyHandle()), "must be");
690   assert(&where != &emptyBucket, "must not try to fill empty bucket");
691   NonPermObject* p = new (arena()) NonPermObject(where, keyHandle(), obj);
692   assert(where == p && is_equal(p, keyHandle()) && p->object() == obj, "entry must match");
693   assert(find_non_perm(keyHandle) == p, "must find the same spot");
694   ++_non_perm_count;
695 }
696 
697 // ------------------------------------------------------------------
698 // ciObjectFactory::vm_symbol_at
699 // Get the ciSymbol corresponding to some index in vmSymbols.
700 ciSymbol* ciObjectFactory::vm_symbol_at(vmSymbolID sid) {
701   int index = vmSymbols::as_int(sid);
702   return _shared_ci_symbols[index];
703 }
704 
705 // ------------------------------------------------------------------
706 // ciObjectFactory::metadata_do
707 void ciObjectFactory::metadata_do(MetadataClosure* f) {
708   for (int j = 0; j < _ci_metadata.length(); j++) {
709     Metadata* o = _ci_metadata.at(j)->constant_encoding();
710     f->do_metadata(o);
711   }
712 }
713 
714 // ------------------------------------------------------------------
715 // ciObjectFactory::print_contents_impl
716 void ciObjectFactory::print_contents_impl() {
717   int len = _ci_metadata.length();
718   tty->print_cr("ciObjectFactory (%d) meta data contents:", len);
719   for (int i = 0; i < len; i++) {
720     _ci_metadata.at(i)->print();
721     tty->cr();
722   }
723 }
724 
725 // ------------------------------------------------------------------
726 // ciObjectFactory::print_contents
727 void ciObjectFactory::print_contents() {
728   print();
729   tty->cr();
730   GUARDED_VM_ENTRY(print_contents_impl();)
731 }
732 
733 // ------------------------------------------------------------------
734 // ciObjectFactory::print
735 //
736 // Print debugging information about the object factory
737 void ciObjectFactory::print() {
738   tty->print("<ciObjectFactory oops=%d metadata=%d unloaded_methods=%d unloaded_instances=%d unloaded_klasses=%d>",
739              _non_perm_count, _ci_metadata.length(), _unloaded_methods.length(),
740              _unloaded_instances.length(),
741              _unloaded_klasses.length());
742 }