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