1 /*
  2  * Copyright (c) 2000, 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_FRAMEMAP_HPP
 26 #define SHARE_C1_C1_FRAMEMAP_HPP
 27 
 28 #include "c1/c1_Defs.hpp"
 29 #include "c1/c1_LIR.hpp"
 30 #include "code/vmreg.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "oops/compressedOops.hpp"
 33 #include "runtime/frame.hpp"
 34 #include "utilities/globalDefinitions.hpp"
 35 #include "utilities/macros.hpp"
 36 
 37 class ciMethod;
 38 class CallingConvention;
 39 
 40 //--------------------------------------------------------
 41 //               FrameMap
 42 //--------------------------------------------------------
 43 
 44 //  This class is responsible of mapping items (locals, monitors, spill
 45 //  slots and registers) to their frame location
 46 //
 47 //  The monitors are specified by a consecutive index, although each monitor entry
 48 //  occupies two words. The monitor_index is 0.._num_monitors
 49 //  The spill index is similar to local index; it is in range 0..(open)
 50 //
 51 //  The CPU registers are mapped using a fixed table; register with number 0
 52 //  is the most used one.
 53 
 54 
 55 //   stack grow direction -->                                        SP
 56 //  +----------+---+----------+-------+------------------------+-----+
 57 //  |arguments | x | monitors | spill | reserved argument area | ABI |
 58 //  +----------+---+----------+-------+------------------------+-----+
 59 //
 60 //  x =  ABI area (SPARC) or  return address and link (i486)
 61 //  ABI  = ABI area (SPARC) or nothing (i486)
 62 
 63 
 64 class FrameMap : public CompilationResourceObj {
 65  public:
 66   enum {
 67     nof_cpu_regs = pd_nof_cpu_regs_frame_map,
 68     nof_fpu_regs = pd_nof_fpu_regs_frame_map,
 69 
 70     nof_cpu_regs_reg_alloc = pd_nof_cpu_regs_reg_alloc,
 71     nof_fpu_regs_reg_alloc = pd_nof_fpu_regs_reg_alloc,
 72 
 73     max_nof_caller_save_cpu_regs = pd_nof_caller_save_cpu_regs_frame_map,
 74     nof_caller_save_fpu_regs     = pd_nof_caller_save_fpu_regs_frame_map,
 75 
 76     spill_slot_size_in_bytes = 4
 77   };
 78 
 79   void update_reserved_argument_area_size (int size) {
 80     assert(size >= 0, "check");
 81     _reserved_argument_area_size = MAX2(_reserved_argument_area_size, size);
 82   }
 83 
 84 #include CPU_HEADER(c1_FrameMap)
 85 
 86   friend class LIR_Opr;
 87 
 88  private:
 89   static bool         _init_done;
 90   static Register     _cpu_rnr2reg [nof_cpu_regs];
 91   static int          _cpu_reg2rnr [nof_cpu_regs];
 92 
 93   static LIR_Opr      _caller_save_cpu_regs [max_nof_caller_save_cpu_regs];
 94   static LIR_Opr      _caller_save_fpu_regs [nof_caller_save_fpu_regs];
 95 
 96   int                 _framesize;
 97   int                 _argcount;
 98   int                 _num_monitors;
 99   int                 _num_spills;
100   int                 _reserved_argument_area_size;
101   int                 _oop_map_arg_count;
102 
103   CallingConvention*  _incoming_arguments;
104   intArray*           _argument_locations;
105 
106   void check_spill_index   (int spill_index)   const { assert(spill_index   >= 0, "bad index"); }
107   void check_monitor_index (int monitor_index) const { assert(monitor_index >= 0 &&
108                                                               monitor_index < _num_monitors, "bad index"); }
109 
110   static Register cpu_rnr2reg (int rnr) {
111     assert(_init_done, "tables not initialized");
112     debug_only(cpu_range_check(rnr);)
113     return _cpu_rnr2reg[rnr];
114   }
115 
116   static int cpu_reg2rnr (Register reg) {
117     assert(_init_done, "tables not initialized");
118     debug_only(cpu_range_check(reg->encoding());)
119     return _cpu_reg2rnr[reg->encoding()];
120   }
121 
122   static void map_register(int rnr, Register reg) {
123     debug_only(cpu_range_check(rnr);)
124     debug_only(cpu_range_check(reg->encoding());)
125     _cpu_rnr2reg[rnr] = reg;
126     _cpu_reg2rnr[reg->encoding()] = rnr;
127   }
128 
129  protected:
130 #ifndef PRODUCT
131   static void cpu_range_check (int rnr)          { assert(0 <= rnr && rnr < nof_cpu_regs, "cpu register number is too big"); }
132   static void fpu_range_check (int rnr)          { assert(0 <= rnr && rnr < nof_fpu_regs, "fpu register number is too big"); }
133 #endif
134 
135   ByteSize sp_offset_for_monitor_base(const int idx) const;
136 
137   Address make_new_address(ByteSize sp_offset) const;
138 
139   ByteSize sp_offset_for_slot(const int idx) const;
140   ByteSize sp_offset_for_double_slot(const int idx) const;
141   ByteSize sp_offset_for_spill(const int idx) const;
142   ByteSize sp_offset_for_monitor_lock(int monitor_index) const;
143   ByteSize sp_offset_for_monitor_object(int monitor_index) const;
144 
145   VMReg sp_offset2vmreg(ByteSize offset) const;
146 
147   // platform dependent hook used to check that frame is properly
148   // addressable on the platform.  Used by arm, ppc to verify that all
149   // stack addresses are valid.
150   bool validate_frame();
151 
152   static LIR_Opr map_to_opr(BasicType type, VMRegPair* reg, bool incoming);
153 
154  public:
155   // Opr representing the stack_pointer on this platform
156   static LIR_Opr stack_pointer();
157 
158   // JSR 292
159   static LIR_Opr method_handle_invoke_SP_save_opr();
160 
161   static BasicTypeArray*     signature_type_array_for(const ciMethod* method);
162 
163   // for outgoing calls, these also update the reserved area to
164   // include space for arguments and any ABI area.
165   CallingConvention* c_calling_convention(const BasicTypeArray* signature);
166   CallingConvention* java_calling_convention(const BasicTypeArray* signature, bool outgoing);
167 
168   // deopt support
169   ByteSize sp_offset_for_orig_pc() { return sp_offset_for_monitor_base(_num_monitors); }
170 
171   static LIR_Opr as_opr(Register r) {
172     return LIR_OprFact::single_cpu(cpu_reg2rnr(r));
173   }
174   static LIR_Opr as_oop_opr(Register r) {
175     return LIR_OprFact::single_cpu_oop(cpu_reg2rnr(r));
176   }
177 
178   static LIR_Opr as_metadata_opr(Register r) {
179     return LIR_OprFact::single_cpu_metadata(cpu_reg2rnr(r));
180   }
181 
182   static LIR_Opr as_address_opr(Register r) {
183     return LIR_OprFact::single_cpu_address(cpu_reg2rnr(r));
184   }
185 
186   FrameMap(ciMethod* method, int monitors, int reserved_argument_area_size);
187   bool finalize_frame(int nof_slots, bool needs_stack_repair);
188 
189   int   reserved_argument_area_size () const     { return _reserved_argument_area_size; }
190   int   framesize                   () const     { assert(_framesize != -1, "hasn't been calculated"); return _framesize; }
191   ByteSize framesize_in_bytes       () const     { return in_ByteSize(framesize() * 4); }
192   int   num_monitors                () const     { return _num_monitors; }
193   int   num_spills                  () const     { assert(_num_spills >= 0, "not set"); return _num_spills; }
194   int   argcount              () const     { assert(_argcount >= 0, "not set"); return _argcount; }
195 
196   int oop_map_arg_count() const { return _oop_map_arg_count; }
197 
198   CallingConvention* incoming_arguments() const  { return _incoming_arguments; }
199 
200   // convenience routines
201   Address address_for_slot(int index, int sp_adjust = 0) const {
202     return make_new_address(sp_offset_for_slot(index) + in_ByteSize(sp_adjust));
203   }
204   Address address_for_double_slot(int index, int sp_adjust = 0) const {
205     return make_new_address(sp_offset_for_double_slot(index) + in_ByteSize(sp_adjust));
206   }
207   Address address_for_monitor_lock(int monitor_index) const {
208     return make_new_address(sp_offset_for_monitor_lock(monitor_index));
209   }
210   Address address_for_monitor_object(int monitor_index) const {
211     return make_new_address(sp_offset_for_monitor_object(monitor_index));
212   }
213   Address address_for_orig_pc_addr() const {
214     return make_new_address(sp_offset_for_monitor_base(_num_monitors));
215   }
216 
217   // Creates Location describing desired slot and returns it via pointer
218   // to Location object. Returns true if the stack frame offset was legal
219   // (as defined by Location::legal_offset_in_bytes()), false otherwise.
220   // Do not use the returned location if this returns false.
221   bool location_for_sp_offset(ByteSize byte_offset_from_sp,
222                               Location::Type loc_type, Location* loc) const;
223 
224   bool location_for_monitor_lock  (int monitor_index, Location* loc) const {
225     return location_for_sp_offset(sp_offset_for_monitor_lock(monitor_index), Location::normal, loc);
226   }
227   bool location_for_monitor_object(int monitor_index, Location* loc) const {
228     return location_for_sp_offset(sp_offset_for_monitor_object(monitor_index), Location::oop, loc);
229   }
230   bool locations_for_slot  (int index, Location::Type loc_type,
231                             Location* loc, Location* second = nullptr) const;
232 
233   VMReg slot_regname(int index) const {
234     return sp_offset2vmreg(sp_offset_for_slot(index));
235   }
236   VMReg monitor_object_regname(int monitor_index) const {
237     return sp_offset2vmreg(sp_offset_for_monitor_object(monitor_index));
238   }
239   VMReg regname(LIR_Opr opr) const;
240 
241   static LIR_Opr caller_save_cpu_reg_at(int i) {
242     assert(i >= 0 && i < max_nof_caller_save_cpu_regs, "out of bounds");
243     return _caller_save_cpu_regs[i];
244   }
245 
246   static LIR_Opr caller_save_fpu_reg_at(int i) {
247     assert(i >= 0 && i < nof_caller_save_fpu_regs, "out of bounds");
248     return _caller_save_fpu_regs[i];
249   }
250 
251   static void initialize();
252 };
253 
254 //               CallingConvention
255 //--------------------------------------------------------
256 
257 class CallingConvention: public ResourceObj {
258  private:
259   LIR_OprList* _args;
260   int          _reserved_stack_slots;
261 
262  public:
263   CallingConvention (LIR_OprList* args, int reserved_stack_slots)
264     : _args(args)
265     , _reserved_stack_slots(reserved_stack_slots)  {}
266 
267   LIR_OprList* args()       { return _args; }
268 
269   LIR_Opr at(int i) const   { return _args->at(i); }
270   int length() const        { return _args->length(); }
271 
272   // Indicates number of real frame slots used by arguments passed on stack.
273   int reserved_stack_slots() const            { return _reserved_stack_slots; }
274 
275 #ifndef PRODUCT
276   void print () const {
277     for (int i = 0; i < length(); i++) {
278       at(i)->print();
279     }
280   }
281 #endif // PRODUCT
282 };
283 
284 #endif // SHARE_C1_C1_FRAMEMAP_HPP