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