1 /*
  2  * Copyright (c) 1999, 2026, 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 #ifndef SHARE_CI_CIOBJECTFACTORY_HPP
 26 #define SHARE_CI_CIOBJECTFACTORY_HPP
 27 
 28 #include "ci/ciClassList.hpp"
 29 #include "ci/ciObject.hpp"
 30 #include "utilities/growableArray.hpp"
 31 #include "utilities/vmEnums.hpp"
 32 
 33 // ciObjectFactory
 34 //
 35 // This class handles requests for the creation of new instances
 36 // of ciObject and its subclasses.  It contains a caching mechanism
 37 // which ensures that for each oop, at most one ciObject is created.
 38 // This invariant allows efficient implementation of ciObject.
 39 class ciObjectFactory : public ArenaObj {
 40   friend class ciEnv;
 41 
 42 private:
 43   static volatile bool _initialized;
 44   static GrowableArray<ciMetadata*>* _shared_ci_metadata;
 45   static ciSymbol*                 _shared_ci_symbols[];
 46   static int                       _shared_ident_limit;
 47 
 48   Arena*                           _arena;
 49   GrowableArray<ciMetadata*>       _ci_metadata;
 50   // Local copy of shared ciInstanceKlass init state for current compilation
 51   GrowableArray<u1>                _cached_init_state;
 52   GrowableArray<ciMethod*>         _unloaded_methods;
 53   GrowableArray<ciKlass*>          _unloaded_klasses;
 54   GrowableArray<ciInstance*>       _unloaded_instances;
 55   GrowableArray<ciReturnAddress*>  _return_addresses;
 56   GrowableArray<ciSymbol*>         _symbols;  // keep list of symbols created
 57   int                              _next_ident;
 58 
 59 public:
 60   struct NonPermObject : public ArenaObj {
 61     ciObject*      _object;
 62     NonPermObject* _next;
 63 
 64     inline NonPermObject(NonPermObject* &bucket, oop key, ciObject* object);
 65     ciObject*     object()  { return _object; }
 66     NonPermObject* &next()  { return _next; }
 67   };
 68 private:
 69   enum { NON_PERM_BUCKETS = 61 };
 70   NonPermObject* _non_perm_bucket[NON_PERM_BUCKETS];
 71   int _non_perm_count;
 72 
 73   static int metadata_compare(Metadata* const& key, ciMetadata* const& elt);
 74 
 75   ciObject* create_new_object(oop o);
 76   ciMetadata* create_new_metadata(Metadata* o);
 77 
 78   static bool is_equal(NonPermObject* p, oop key) {
 79     return p->object()->get_oop() == key;
 80   }
 81 
 82   NonPermObject* &find_non_perm(Handle keyHandle);
 83   void insert_non_perm(NonPermObject* &where, Handle keyHandle, ciObject* obj);
 84 
 85   void init_ident_of(ciBaseObject* obj);
 86 
 87   Arena* arena() { return _arena; }
 88 
 89   void print_contents_impl();
 90 
 91   ciInstance* get_unloaded_instance(ciInstanceKlass* klass);
 92 
 93 public:
 94   static bool is_initialized() { return _initialized; }
 95 
 96   static void initialize();
 97   void init_shared_objects();
 98   void remove_symbols();
 99 
100   ciObjectFactory(Arena* arena, int expected_size);
101 
102   // Get the ciObject corresponding to some oop.
103   ciObject* get(oop key);
104   ciMetadata* get_metadata(Metadata* key);
105   ciMetadata* cached_metadata(Metadata* key);
106   ciSymbol* get_symbol(Symbol* key);
107 
108   // Get cached init state of shared ciInstanceKlass
109   u1 cached_init_state(uint id) {
110     return _cached_init_state.at(id);
111   }
112 
113   // Get the ciSymbol corresponding to one of the vmSymbols.
114   static ciSymbol* vm_symbol_at(vmSymbolID index);
115 
116   // Called on every new object made.
117   void notice_new_object(ciBaseObject* new_object);
118 
119   // Get the ciMethod representing an unloaded/unfound method.
120   ciMethod* get_unloaded_method(ciInstanceKlass* holder,
121                                 ciSymbol*        name,
122                                 ciSymbol*        signature,
123                                 ciInstanceKlass* accessor);
124 
125   // Get a ciKlass representing an unloaded klass.
126   ciKlass* get_unloaded_klass(ciKlass* accessing_klass,
127                               ciSymbol* name,
128                               bool create_if_not_found);
129 
130   // Get a ciInstance representing an unresolved klass mirror.
131   ciInstance* get_unloaded_klass_mirror(ciKlass* type);
132 
133   // Get a ciInstance representing an unresolved method handle constant.
134   ciInstance* get_unloaded_method_handle_constant(ciKlass*  holder,
135                                                   ciSymbol* name,
136                                                   ciSymbol* signature,
137                                                   int       ref_kind);
138 
139   // Get a ciInstance representing an unresolved method type constant.
140   ciInstance* get_unloaded_method_type_constant(ciSymbol* signature);
141 
142 
143   ciInstance* get_unloaded_object_constant();
144 
145   // Get the ciMethodData representing the methodData for a method
146   // with none.
147   ciMethodData* get_empty_methodData();
148 
149   ciReturnAddress* get_return_address(int bci);
150 
151   GrowableArray<ciMetadata*>* get_ci_metadata() { return &_ci_metadata; }
152   // RedefineClasses support
153   void metadata_do(MetadataClosure* f);
154 
155   void print_contents();
156   void print();
157 };
158 
159 #endif // SHARE_CI_CIOBJECTFACTORY_HPP