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 #include "precompiled.hpp"
  26 #include "c1/c1_FrameMap.hpp"
  27 #include "c1/c1_LIR.hpp"
  28 #include "runtime/sharedRuntime.hpp"
  29 #ifdef TARGET_ARCH_x86
  30 # include "vmreg_x86.inline.hpp"
  31 #endif
  32 #ifdef TARGET_ARCH_sparc
  33 # include "vmreg_sparc.inline.hpp"
  34 #endif
  35 #ifdef TARGET_ARCH_zero
  36 # include "vmreg_zero.inline.hpp"
  37 #endif
  38 
  39 
  40 
  41 //-----------------------------------------------------
  42 
  43 // Convert method signature into an array of BasicTypes for the arguments
  44 BasicTypeArray* FrameMap::signature_type_array_for(const ciMethod* method) {
  45   ciSignature* sig = method->signature();
  46   BasicTypeList* sta = new BasicTypeList(method->arg_size());
  47   // add receiver, if any
  48   if (!method->is_static()) sta->append(T_OBJECT);
  49   // add remaining arguments
  50   for (int i = 0; i < sig->count(); i++) {
  51     ciType* type = sig->type_at(i);
  52     BasicType t = type->basic_type();
  53     if (t == T_ARRAY) {
  54       t = T_OBJECT;
  55     }
  56     sta->append(t);
  57   }
  58   // done
  59   return sta;
  60 }
  61 
  62 
  63 CallingConvention* FrameMap::java_calling_convention(const BasicTypeArray* signature, bool outgoing) {
  64   // compute the size of the arguments first.  The signature array
  65   // that java_calling_convention takes includes a T_VOID after double
  66   // work items but our signatures do not.
  67   int i;
  68   int sizeargs = 0;
  69   for (i = 0; i < signature->length(); i++) {
  70     sizeargs += type2size[signature->at(i)];
  71   }
  72 
  73   BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
  74   VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
  75   int sig_index = 0;
  76   for (i = 0; i < sizeargs; i++, sig_index++) {
  77     sig_bt[i] = signature->at(sig_index);
  78     if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
  79       sig_bt[i + 1] = T_VOID;
  80       i++;
  81     }
  82   }
  83 
  84   intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, outgoing);
  85   LIR_OprList* args = new LIR_OprList(signature->length());
  86   for (i = 0; i < sizeargs;) {
  87     BasicType t = sig_bt[i];
  88     assert(t != T_VOID, "should be skipping these");
  89 
  90     LIR_Opr opr = map_to_opr(t, regs + i, outgoing);
  91     args->append(opr);
  92     if (opr->is_address()) {
  93       LIR_Address* addr = opr->as_address_ptr();
  94       assert(addr->disp() == (int)addr->disp(), "out of range value");
  95       out_preserve = MAX2(out_preserve, (intptr_t)addr->disp() / 4);
  96     }
  97     i += type2size[t];
  98   }
  99   assert(args->length() == signature->length(), "size mismatch");
 100   out_preserve += SharedRuntime::out_preserve_stack_slots();
 101 
 102   if (outgoing) {
 103     // update the space reserved for arguments.
 104     update_reserved_argument_area_size(out_preserve * BytesPerWord);
 105   }
 106   return new CallingConvention(args, out_preserve);
 107 }
 108 
 109 
 110 CallingConvention* FrameMap::c_calling_convention(const BasicTypeArray* signature) {
 111   // compute the size of the arguments first.  The signature array
 112   // that java_calling_convention takes includes a T_VOID after double
 113   // work items but our signatures do not.
 114   int i;
 115   int sizeargs = 0;
 116   for (i = 0; i < signature->length(); i++) {
 117     sizeargs += type2size[signature->at(i)];
 118   }
 119 
 120   BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
 121   VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
 122   int sig_index = 0;
 123   for (i = 0; i < sizeargs; i++, sig_index++) {
 124     sig_bt[i] = signature->at(sig_index);
 125     if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
 126       sig_bt[i + 1] = T_VOID;
 127       i++;
 128     }
 129   }
 130 
 131   intptr_t out_preserve = SharedRuntime::c_calling_convention(sig_bt, regs, sizeargs);
 132   LIR_OprList* args = new LIR_OprList(signature->length());
 133   for (i = 0; i < sizeargs;) {
 134     BasicType t = sig_bt[i];
 135     assert(t != T_VOID, "should be skipping these");
 136 
 137     // C calls are always outgoing
 138     bool outgoing = true;
 139     LIR_Opr opr = map_to_opr(t, regs + i, outgoing);
 140     // they might be of different types if for instance floating point
 141     // values are passed in cpu registers, but the sizes must match.
 142     assert(type2size[opr->type()] == type2size[t], "type mismatch");
 143     args->append(opr);
 144     if (opr->is_address()) {
 145       LIR_Address* addr = opr->as_address_ptr();
 146       out_preserve = MAX2(out_preserve, (intptr_t)addr->disp() / 4);
 147     }
 148     i += type2size[t];
 149   }
 150   assert(args->length() == signature->length(), "size mismatch");
 151   out_preserve += SharedRuntime::out_preserve_stack_slots();
 152   update_reserved_argument_area_size(out_preserve * BytesPerWord);
 153   return new CallingConvention(args, out_preserve);
 154 }
 155 
 156 
 157 //--------------------------------------------------------
 158 //               FrameMap
 159 //--------------------------------------------------------
 160 
 161 bool      FrameMap::_init_done = false;
 162 Register  FrameMap::_cpu_rnr2reg [FrameMap::nof_cpu_regs];
 163 int       FrameMap::_cpu_reg2rnr [FrameMap::nof_cpu_regs];
 164 
 165 
 166 FrameMap::FrameMap(ciMethod* method, int monitors, int reserved_argument_area_size) {
 167   assert(_init_done, "should already be completed");
 168 
 169   _framesize = -1;
 170   _num_spills = -1;
 171 
 172   assert(monitors >= 0, "not set");
 173   _num_monitors = monitors;
 174   assert(reserved_argument_area_size >= 0, "not set");
 175   _reserved_argument_area_size = MAX2(4, reserved_argument_area_size) * BytesPerWord;
 176 
 177   _argcount = method->arg_size();
 178   _argument_locations = new intArray(_argcount, -1);
 179   _incoming_arguments = java_calling_convention(signature_type_array_for(method), false);
 180   _oop_map_arg_count = _incoming_arguments->reserved_stack_slots();
 181 
 182   int java_index = 0;
 183   for (int i = 0; i < _incoming_arguments->length(); i++) {
 184     LIR_Opr opr = _incoming_arguments->at(i);
 185     if (opr->is_address()) {
 186       LIR_Address* address = opr->as_address_ptr();
 187       _argument_locations->at_put(java_index, address->disp() - STACK_BIAS);
 188       _incoming_arguments->args()->at_put(i, LIR_OprFact::stack(java_index, as_BasicType(as_ValueType(address->type()))));
 189     }
 190     java_index += type2size[opr->type()];
 191   }
 192 
 193 }
 194 
 195 
 196 bool FrameMap::finalize_frame(int nof_slots) {
 197   assert(nof_slots >= 0, "must be positive");
 198   assert(_num_spills == -1, "can only be set once");
 199   _num_spills = nof_slots;
 200   assert(_framesize == -1, "should only be calculated once");
 201   _framesize =  round_to(in_bytes(sp_offset_for_monitor_base(0)) +
 202                          _num_monitors * sizeof(BasicObjectLock) +
 203                          sizeof(intptr_t) +                        // offset of deopt orig pc
 204                          frame_pad_in_bytes,
 205                          StackAlignmentInBytes) / 4;
 206   int java_index = 0;
 207   for (int i = 0; i < _incoming_arguments->length(); i++) {
 208     LIR_Opr opr = _incoming_arguments->at(i);
 209     if (opr->is_stack()) {
 210       _argument_locations->at_put(java_index, in_bytes(framesize_in_bytes()) +
 211                                   _argument_locations->at(java_index));
 212     }
 213     java_index += type2size[opr->type()];
 214   }
 215   // make sure it's expressible on the platform
 216   return validate_frame();
 217 }
 218 
 219 VMReg FrameMap::sp_offset2vmreg(ByteSize offset) const {
 220   int offset_in_bytes = in_bytes(offset);
 221   assert(offset_in_bytes % 4 == 0, "must be multiple of 4 bytes");
 222   assert(offset_in_bytes / 4 < framesize() + oop_map_arg_count(), "out of range");
 223   return VMRegImpl::stack2reg(offset_in_bytes / 4);
 224 }
 225 
 226 
 227 bool FrameMap::location_for_sp_offset(ByteSize byte_offset_from_sp,
 228                                       Location::Type loc_type,
 229                                       Location* loc) const {
 230   int offset = in_bytes(byte_offset_from_sp);
 231   assert(offset >= 0, "incorrect offset");
 232   if (!Location::legal_offset_in_bytes(offset)) {
 233     return false;
 234   }
 235   Location tmp_loc = Location::new_stk_loc(loc_type, offset);
 236   *loc = tmp_loc;
 237   return true;
 238 }
 239 
 240 
 241 bool FrameMap::locations_for_slot  (int index, Location::Type loc_type,
 242                                      Location* loc, Location* second) const {
 243   ByteSize offset_from_sp = sp_offset_for_slot(index);
 244   if (!location_for_sp_offset(offset_from_sp, loc_type, loc)) {
 245     return false;
 246   }
 247   if (second != NULL) {
 248     // two word item
 249     offset_from_sp = offset_from_sp + in_ByteSize(4);
 250     return location_for_sp_offset(offset_from_sp, loc_type, second);
 251   }
 252   return true;
 253 }
 254 
 255 //////////////////////
 256 // Public accessors //
 257 //////////////////////
 258 
 259 
 260 ByteSize FrameMap::sp_offset_for_slot(const int index) const {
 261   if (index < argcount()) {
 262     int offset = _argument_locations->at(index);
 263     assert(offset != -1, "not a memory argument");
 264     assert(offset >= framesize() * 4, "argument inside of frame");
 265     return in_ByteSize(offset);
 266   }
 267   ByteSize offset = sp_offset_for_spill(index - argcount());
 268   assert(in_bytes(offset) < framesize() * 4, "spill outside of frame");
 269   return offset;
 270 }
 271 
 272 
 273 ByteSize FrameMap::sp_offset_for_double_slot(const int index) const {
 274   ByteSize offset = sp_offset_for_slot(index);
 275   if (index >= argcount()) {
 276     assert(in_bytes(offset) + 4 < framesize() * 4, "spill outside of frame");
 277   }
 278   return offset;
 279 }
 280 
 281 
 282 ByteSize FrameMap::sp_offset_for_spill(const int index) const {
 283   assert(index >= 0 && index < _num_spills, "out of range");
 284   int offset = round_to(first_available_sp_in_frame + _reserved_argument_area_size, sizeof(double)) +
 285     index * spill_slot_size_in_bytes;
 286   return in_ByteSize(offset);
 287 }
 288 
 289 ByteSize FrameMap::sp_offset_for_monitor_base(const int index) const {
 290   int end_of_spills = round_to(first_available_sp_in_frame + _reserved_argument_area_size, sizeof(double)) +
 291     _num_spills * spill_slot_size_in_bytes;
 292   int offset = (int) round_to(end_of_spills, HeapWordSize) + index * sizeof(BasicObjectLock);
 293   return in_ByteSize(offset);
 294 }
 295 
 296 ByteSize FrameMap::sp_offset_for_monitor_lock(int index) const {
 297   check_monitor_index(index);
 298   return sp_offset_for_monitor_base(index) + in_ByteSize(BasicObjectLock::lock_offset_in_bytes());;
 299 }
 300 
 301 ByteSize FrameMap::sp_offset_for_monitor_object(int index) const {
 302   check_monitor_index(index);
 303   return sp_offset_for_monitor_base(index) + in_ByteSize(BasicObjectLock::obj_offset_in_bytes());
 304 }
 305 
 306 void FrameMap::print_frame_layout() const {
 307   int svar;
 308   tty->print_cr("#####################################");
 309   tty->print_cr("Frame size in words %d", framesize());
 310 
 311   if( _num_monitors > 0) {
 312     tty->print_cr("monitor [0]:%d | [%2d]:%d",
 313                   in_bytes(sp_offset_for_monitor_base(0)),
 314                   in_bytes(sp_offset_for_monitor_base(_num_monitors)));
 315   }
 316   if( _num_spills > 0) {
 317     svar = _num_spills - 1;
 318     if(svar == 0)
 319       tty->print_cr("spill   [0]:%d", in_bytes(sp_offset_for_spill(0)));
 320     else
 321       tty->print_cr("spill   [0]:%d | [%2d]:%d", in_bytes(sp_offset_for_spill(0)),
 322                     svar,
 323                     in_bytes(sp_offset_for_spill(svar)));
 324   }
 325 }
 326 
 327 
 328 // For OopMaps, map a local variable or spill index to an VMReg.
 329 // This is the offset from sp() in the frame of the slot for the index,
 330 // skewed by SharedInfo::stack0 to indicate a stack location (vs.a register.)
 331 //
 332 //         C ABI size +
 333 //         framesize +     framesize +
 334 //         stack0          stack0         stack0          0 <- VMReg->value()
 335 //            |              |              | <registers> |
 336 //  ..........|..............|..............|.............|
 337 //    0 1 2 3 | <C ABI area> | 4 5 6 ...... |               <- local indices
 338 //    ^                        ^          sp()
 339 //    |                        |
 340 //  arguments            non-argument locals
 341 
 342 
 343 VMReg FrameMap::regname(LIR_Opr opr) const {
 344   if (opr->is_single_cpu()) {
 345     assert(!opr->is_virtual(), "should not see virtual registers here");
 346     return opr->as_register()->as_VMReg();
 347   } else if (opr->is_single_stack()) {
 348     return sp_offset2vmreg(sp_offset_for_slot(opr->single_stack_ix()));
 349   } else if (opr->is_address()) {
 350     LIR_Address* addr = opr->as_address_ptr();
 351     assert(addr->base() == stack_pointer(), "sp based addressing only");
 352     return sp_offset2vmreg(in_ByteSize(addr->index()->as_jint()));
 353   }
 354   ShouldNotReachHere();
 355   return VMRegImpl::Bad();
 356 }
 357 
 358 
 359 
 360 
 361 // ------------ extra spill slots ---------------