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