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