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