1 /*
   2  * Copyright 2000-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any 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   static BasicTypeArray*     signature_type_array_for(const ciMethod* method);
 154   static BasicTypeArray*     signature_type_array_for(const char * signature);
 155 
 156   // for outgoing calls, these also update the reserved area to
 157   // include space for arguments and any ABI area.
 158   CallingConvention* c_calling_convention (const BasicTypeArray* signature);
 159   CallingConvention* java_calling_convention (const BasicTypeArray* signature, bool outgoing);
 160 
 161   // deopt support
 162   ByteSize sp_offset_for_orig_pc() { return sp_offset_for_monitor_base(_num_monitors); }
 163 
 164   static LIR_Opr as_opr(Register r) {
 165     return LIR_OprFact::single_cpu(cpu_reg2rnr(r));
 166   }
 167   static LIR_Opr as_oop_opr(Register r) {
 168     return LIR_OprFact::single_cpu_oop(cpu_reg2rnr(r));
 169   }
 170 
 171   FrameMap(ciMethod* method, int monitors, int reserved_argument_area_size);
 172   bool finalize_frame(int nof_slots);
 173 
 174   int   reserved_argument_area_size () const     { return _reserved_argument_area_size; }
 175   int   framesize                   () const     { assert(_framesize != -1, "hasn't been calculated"); return _framesize; }
 176   ByteSize framesize_in_bytes       () const     { return in_ByteSize(framesize() * 4); }
 177   int   num_monitors                () const     { return _num_monitors; }
 178   int   num_spills                  () const     { assert(_num_spills >= 0, "not set"); return _num_spills; }
 179   int   argcount              () const     { assert(_argcount >= 0, "not set"); return _argcount; }
 180 
 181   int oop_map_arg_count() const { return _oop_map_arg_count; }
 182 
 183   CallingConvention* incoming_arguments() const  { return _incoming_arguments; }
 184 
 185   // convenience routines
 186   Address address_for_slot(int index, int sp_adjust = 0) const {
 187     return make_new_address(sp_offset_for_slot(index) + in_ByteSize(sp_adjust));
 188   }
 189   Address address_for_double_slot(int index, int sp_adjust = 0) const {
 190     return make_new_address(sp_offset_for_double_slot(index) + in_ByteSize(sp_adjust));
 191   }
 192   Address address_for_monitor_lock(int monitor_index) const {
 193     return make_new_address(sp_offset_for_monitor_lock(monitor_index));
 194   }
 195   Address address_for_monitor_object(int monitor_index) const {
 196     return make_new_address(sp_offset_for_monitor_object(monitor_index));
 197   }
 198 
 199   void print_frame_layout() const;
 200 
 201   // Creates Location describing desired slot and returns it via pointer
 202   // to Location object. Returns true if the stack frame offset was legal
 203   // (as defined by Location::legal_offset_in_bytes()), false otherwise.
 204   // Do not use the returned location if this returns false.
 205   bool location_for_sp_offset(ByteSize byte_offset_from_sp,
 206                               Location::Type loc_type, Location* loc) const;
 207 
 208   bool location_for_monitor_lock  (int monitor_index, Location* loc) const {
 209     return location_for_sp_offset(sp_offset_for_monitor_lock(monitor_index), Location::normal, loc);
 210   }
 211   bool location_for_monitor_object(int monitor_index, Location* loc) const {
 212     return location_for_sp_offset(sp_offset_for_monitor_object(monitor_index), Location::oop, loc);
 213   }
 214   bool locations_for_slot  (int index, Location::Type loc_type,
 215                             Location* loc, Location* second = NULL) const;
 216 
 217   VMReg slot_regname(int index) const {
 218     return sp_offset2vmreg(sp_offset_for_slot(index));
 219   }
 220   VMReg monitor_object_regname(int monitor_index) const {
 221     return sp_offset2vmreg(sp_offset_for_monitor_object(monitor_index));
 222   }
 223   VMReg regname(LIR_Opr opr) const;
 224 
 225   static LIR_Opr caller_save_cpu_reg_at(int i) {
 226     assert(i >= 0 && i < nof_caller_save_cpu_regs, "out of bounds");
 227     return _caller_save_cpu_regs[i];
 228   }
 229 
 230   static LIR_Opr caller_save_fpu_reg_at(int i) {
 231     assert(i >= 0 && i < nof_caller_save_fpu_regs, "out of bounds");
 232     return _caller_save_fpu_regs[i];
 233   }
 234 
 235   static void init();
 236 };
 237 
 238 //               CallingConvention
 239 //--------------------------------------------------------
 240 
 241 class CallingConvention: public ResourceObj {
 242  private:
 243   LIR_OprList* _args;
 244   int          _reserved_stack_slots;
 245 
 246  public:
 247   CallingConvention (LIR_OprList* args, int reserved_stack_slots)
 248     : _args(args)
 249     , _reserved_stack_slots(reserved_stack_slots)  {}
 250 
 251   LIR_OprList* args()       { return _args; }
 252 
 253   LIR_Opr at(int i) const   { return _args->at(i); }
 254   int length() const        { return _args->length(); }
 255 
 256   // Indicates number of real frame slots used by arguments passed on stack.
 257   int reserved_stack_slots() const            { return _reserved_stack_slots; }
 258 
 259 #ifndef PRODUCT
 260   void print () const {
 261     for (int i = 0; i < length(); i++) {
 262       at(i)->print();
 263     }
 264   }
 265 #endif // PRODUCT
 266 };