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 #ifndef SHARE_C1_C1_RUNTIME1_HPP
 26 #define SHARE_C1_C1_RUNTIME1_HPP
 27 
 28 #include "c1/c1_FrameMap.hpp"
 29 #include "code/stubs.hpp"
 30 #include "interpreter/interpreter.hpp"
 31 #include "memory/allStatic.hpp"
 32 #include "runtime/stubDeclarations.hpp"
 33 
 34 class StubAssembler;
 35 
 36 // The Runtime1 holds all assembly stubs and VM
 37 // runtime routines needed by code code generated
 38 // by the Compiler1.
 39 
 40 class StubAssemblerCodeGenClosure: public Closure {
 41  public:
 42   virtual OopMapSet* generate_code(StubAssembler* sasm) = 0;
 43 };
 44 
 45 // define C1StubId enum tags: unwind_exception_id etc
 46 
 47 #define C1_STUB_ID_ENUM_DECLARE(name) STUB_ID_NAME(name),
 48 enum class C1StubId :int {
 49   NO_STUBID = -1,
 50   C1_STUBS_DO(C1_STUB_ID_ENUM_DECLARE)
 51   NUM_STUBIDS
 52 };
 53 #undef C1_STUB_ID_ENUM_DECLARE
 54 
 55 class Runtime1: public AllStatic {
 56   friend class ArrayCopyStub;
 57 
 58 public:
 59   // statistics
 60 #ifndef PRODUCT
 61   static uint _generic_arraycopystub_cnt;
 62   static uint _arraycopy_slowcase_cnt;
 63   static uint _arraycopy_checkcast_cnt;
 64   static uint _arraycopy_checkcast_attempt_cnt;
 65   static uint _new_type_array_slowcase_cnt;
 66   static uint _new_object_array_slowcase_cnt;
 67   static uint _new_instance_slowcase_cnt;
 68   static uint _new_multi_array_slowcase_cnt;
 69   static uint _monitorenter_slowcase_cnt;
 70   static uint _monitorexit_slowcase_cnt;
 71   static uint _patch_code_slowcase_cnt;
 72   static uint _throw_range_check_exception_count;
 73   static uint _throw_index_exception_count;
 74   static uint _throw_div0_exception_count;
 75   static uint _throw_null_pointer_exception_count;
 76   static uint _throw_class_cast_exception_count;
 77   static uint _throw_incompatible_class_change_error_count;
 78   static uint _throw_count;
 79 #endif
 80 
 81  private:
 82   static CodeBlob* _blobs[(int)C1StubId::NUM_STUBIDS];
 83   static const char* _blob_names[];
 84 
 85   // stub generation
 86  public:
 87   static CodeBlob*  generate_blob(BufferBlob* buffer_blob, C1StubId id, const char* name, bool expect_oop_map, StubAssemblerCodeGenClosure *cl);
 88   static bool       generate_blob_for(BufferBlob* blob, C1StubId id);
 89   static OopMapSet* generate_code_for(C1StubId id, StubAssembler* sasm);
 90  private:
 91   static OopMapSet* generate_exception_throw(StubAssembler* sasm, address target, bool has_argument);
 92   static OopMapSet* generate_handle_exception(C1StubId id, StubAssembler* sasm);
 93   static void       generate_unwind_exception(StubAssembler *sasm);
 94   static OopMapSet* generate_patching(StubAssembler* sasm, address target);
 95 
 96   static OopMapSet* generate_stub_call(StubAssembler* sasm, Register result, address entry,
 97                                        Register arg1 = noreg, Register arg2 = noreg, Register arg3 = noreg);
 98 
 99   // runtime entry points
100   static void new_instance    (JavaThread* current, Klass* klass);
101   static void new_type_array  (JavaThread* current, Klass* klass, jint length);
102   static void new_object_array(JavaThread* current, Klass* klass, jint length);
103   static void new_multi_array (JavaThread* current, Klass* klass, int rank, jint* dims);
104 
105   static address counter_overflow(JavaThread* current, int bci, Method* method);
106 
107   static void unimplemented_entry(JavaThread* current, C1StubId id);
108 
109   static address exception_handler_for_pc(JavaThread* current);
110 
111   static void throw_range_check_exception(JavaThread* current, int index, arrayOopDesc* a);
112   static void throw_index_exception(JavaThread* current, int index);
113   static void throw_div0_exception(JavaThread* current);
114   static void throw_null_pointer_exception(JavaThread* current);
115   static void throw_class_cast_exception(JavaThread* current, oopDesc* object);
116   static void throw_incompatible_class_change_error(JavaThread* current);
117   static void throw_array_store_exception(JavaThread* current, oopDesc* object);
118 
119   static void monitorenter(JavaThread* current, oopDesc* obj, BasicObjectLock* lock);
120   static void monitorexit (JavaThread* current, BasicObjectLock* lock);
121 
122   static void deoptimize(JavaThread* current, jint trap_request);
123 
124   static int access_field_patching(JavaThread* current);
125   static int move_klass_patching(JavaThread* current);
126   static int move_mirror_patching(JavaThread* current);
127   static int move_appendix_patching(JavaThread* current);
128 
129   static void patch_code(JavaThread* current, C1StubId stub_id);
130 
131  public:
132   // initialization
133   static bool initialize(BufferBlob* blob);
134   static void initialize_pd();
135 
136   // return offset in words
137   static uint runtime_blob_current_thread_offset(frame f);
138 
139   // stubs
140   static CodeBlob* blob_for (C1StubId id);
141   static address   entry_for(C1StubId id)          { return blob_for(id)->code_begin(); }
142   static const char* name_for (C1StubId id);
143   static const char* name_for_address(address entry);
144 
145   // platform might add runtime names.
146   static const char* pd_name_for_address(address entry);
147 
148   // method tracing
149   static void trace_block_entry(jint block_id);
150 
151 #ifndef PRODUCT
152   static address throw_count_address()               { return (address)&_throw_count;             }
153   static address arraycopy_count_address(BasicType type);
154 #endif
155 
156   // directly accessible leaf routine
157   static int  is_instance_of(oopDesc* mirror, oopDesc* obj);
158 
159   static void predicate_failed_trap(JavaThread* current);
160 
161   static void check_abort_on_vm_exception(oopDesc* ex);
162 
163   static void print_statistics_on(outputStream* st) PRODUCT_RETURN;
164 
165   static void init_counters();
166   static void print_counters_on(outputStream* st);
167 };
168 
169 #endif // SHARE_C1_C1_RUNTIME1_HPP