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