1 /*
   2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 2009 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciMethod.hpp"
  28 #include "code/debugInfoRec.hpp"
  29 #include "shark/llvmValue.hpp"
  30 #include "shark/sharkBuilder.hpp"
  31 #include "shark/sharkCacheDecache.hpp"
  32 #include "shark/sharkFunction.hpp"
  33 #include "shark/sharkState.hpp"
  34 
  35 using namespace llvm;
  36 
  37 void SharkDecacher::start_frame() {
  38   // Start recording the debug information
  39   _pc_offset = code_buffer()->create_unique_offset();
  40   _oopmap = new OopMap(
  41     oopmap_slot_munge(stack()->oopmap_frame_size()),
  42     oopmap_slot_munge(arg_size()));
  43   debug_info()->add_safepoint(pc_offset(), oopmap());
  44 }
  45 
  46 void SharkDecacher::start_stack(int stack_depth) {
  47   // Create the array we'll record our stack slots in
  48   _exparray = new GrowableArray<ScopeValue*>(stack_depth);
  49 
  50   // Set the stack pointer
  51   stack()->CreateStoreStackPointer(
  52     builder()->CreatePtrToInt(
  53       stack()->slot_addr(
  54         stack()->stack_slots_offset() + max_stack() - stack_depth),
  55       SharkType::intptr_type()));
  56 }
  57 
  58 void SharkDecacher::process_stack_slot(int          index,
  59                                        SharkValue** addr,
  60                                        int          offset) {
  61   SharkValue *value = *addr;
  62 
  63   // Write the value to the frame if necessary
  64   if (stack_slot_needs_write(index, value)) {
  65     write_value_to_frame(
  66       SharkType::to_stackType(value->basic_type()),
  67       value->generic_value(),
  68       adjusted_offset(value, offset));
  69   }
  70 
  71   // Record the value in the oopmap if necessary
  72   if (stack_slot_needs_oopmap(index, value)) {
  73     oopmap()->set_oop(slot2reg(offset));
  74   }
  75 
  76   // Record the value in the debuginfo if necessary
  77   if (stack_slot_needs_debuginfo(index, value)) {
  78     exparray()->append(slot2lv(offset, stack_location_type(index, addr)));
  79   }
  80 }
  81 
  82 void SharkDecacher::start_monitors(int num_monitors) {
  83   // Create the array we'll record our monitors in
  84   _monarray = new GrowableArray<MonitorValue*>(num_monitors);
  85 }
  86 
  87 void SharkDecacher::process_monitor(int index, int box_offset, int obj_offset) {
  88   oopmap()->set_oop(slot2reg(obj_offset));
  89 
  90   monarray()->append(new MonitorValue(
  91     slot2lv (obj_offset, Location::oop),
  92     slot2loc(box_offset, Location::normal)));
  93 }
  94 
  95 void SharkDecacher::process_oop_tmp_slot(Value** value, int offset) {
  96   // Decache the temporary oop slot
  97   if (*value) {
  98     write_value_to_frame(
  99       SharkType::oop_type(),
 100       *value,
 101       offset);
 102 
 103     oopmap()->set_oop(slot2reg(offset));
 104   }
 105 }
 106 
 107 void SharkDecacher::process_method_slot(Value** value, int offset) {
 108   // Decache the method pointer
 109   write_value_to_frame(
 110     SharkType::Method_type(),
 111     *value,
 112     offset);
 113 
 114 }
 115 
 116 void SharkDecacher::process_pc_slot(int offset) {
 117   // Record the PC
 118   builder()->CreateStore(
 119     builder()->code_buffer_address(pc_offset()),
 120     stack()->slot_addr(offset));
 121 }
 122 
 123 void SharkDecacher::start_locals() {
 124   // Create the array we'll record our local variables in
 125   _locarray = new GrowableArray<ScopeValue*>(max_locals());}
 126 
 127 void SharkDecacher::process_local_slot(int          index,
 128                                        SharkValue** addr,
 129                                        int          offset) {
 130   SharkValue *value = *addr;
 131 
 132   // Write the value to the frame if necessary
 133   if (local_slot_needs_write(index, value)) {
 134     write_value_to_frame(
 135       SharkType::to_stackType(value->basic_type()),
 136       value->generic_value(),
 137       adjusted_offset(value, offset));
 138   }
 139 
 140   // Record the value in the oopmap if necessary
 141   if (local_slot_needs_oopmap(index, value)) {
 142     oopmap()->set_oop(slot2reg(offset));
 143   }
 144 
 145   // Record the value in the debuginfo if necessary
 146   if (local_slot_needs_debuginfo(index, value)) {
 147     locarray()->append(slot2lv(offset, local_location_type(index, addr)));
 148   }
 149 }
 150 
 151 void SharkDecacher::end_frame() {
 152   // Record the scope
 153   methodHandle null_mh;
 154   debug_info()->describe_scope(
 155     pc_offset(),
 156     null_mh,
 157     target(),
 158     bci(),
 159     true,
 160     false,
 161     false,
 162     debug_info()->create_scope_values(locarray()),
 163     debug_info()->create_scope_values(exparray()),
 164     debug_info()->create_monitor_values(monarray()));
 165 
 166   // Finish recording the debug information
 167   debug_info()->end_safepoint(pc_offset());
 168 }
 169 
 170 void SharkCacher::process_stack_slot(int          index,
 171                                      SharkValue** addr,
 172                                      int          offset) {
 173   SharkValue *value = *addr;
 174 
 175   // Read the value from the frame if necessary
 176   if (stack_slot_needs_read(index, value)) {
 177     *addr = SharkValue::create_generic(
 178       value->type(),
 179       read_value_from_frame(
 180         SharkType::to_stackType(value->basic_type()),
 181         adjusted_offset(value, offset)),
 182       value->zero_checked());
 183   }
 184 }
 185 
 186 void SharkOSREntryCacher::process_monitor(int index,
 187                                           int box_offset,
 188                                           int obj_offset) {
 189   // Copy the monitor from the OSR buffer to the frame
 190   int src_offset = max_locals() + index * 2;
 191   builder()->CreateStore(
 192     builder()->CreateLoad(
 193       CreateAddressOfOSRBufEntry(src_offset, SharkType::intptr_type())),
 194     stack()->slot_addr(box_offset, SharkType::intptr_type()));
 195   builder()->CreateStore(
 196     builder()->CreateLoad(
 197       CreateAddressOfOSRBufEntry(src_offset + 1, SharkType::oop_type())),
 198     stack()->slot_addr(obj_offset, SharkType::oop_type()));
 199 }
 200 
 201 void SharkCacher::process_oop_tmp_slot(Value** value, int offset) {
 202   // Cache the temporary oop
 203   if (*value)
 204     *value = read_value_from_frame(SharkType::oop_type(), offset);
 205 }
 206 
 207 void SharkCacher::process_method_slot(Value** value, int offset) {
 208   // Cache the method pointer
 209   *value = read_value_from_frame(SharkType::Method_type(), offset);
 210 }
 211 
 212 void SharkFunctionEntryCacher::process_method_slot(Value** value, int offset) {
 213   // "Cache" the method pointer
 214   *value = method();
 215 }
 216 
 217 void SharkCacher::process_local_slot(int          index,
 218                                      SharkValue** addr,
 219                                      int          offset) {
 220   SharkValue *value = *addr;
 221 
 222   // Read the value from the frame if necessary
 223   if (local_slot_needs_read(index, value)) {
 224     *addr = SharkValue::create_generic(
 225       value->type(),
 226       read_value_from_frame(
 227         SharkType::to_stackType(value->basic_type()),
 228         adjusted_offset(value, offset)),
 229       value->zero_checked());
 230   }
 231 }
 232 
 233 Value* SharkOSREntryCacher::CreateAddressOfOSRBufEntry(int         offset,
 234                                                        Type* type) {
 235   Value *result = builder()->CreateStructGEP(osr_buf(), offset);
 236   if (type != SharkType::intptr_type())
 237     result = builder()->CreateBitCast(result, PointerType::getUnqual(type));
 238   return result;
 239 }
 240 
 241 void SharkOSREntryCacher::process_local_slot(int          index,
 242                                              SharkValue** addr,
 243                                              int          offset) {
 244   SharkValue *value = *addr;
 245 
 246   // Read the value from the OSR buffer if necessary
 247   if (local_slot_needs_read(index, value)) {
 248     *addr = SharkValue::create_generic(
 249       value->type(),
 250       builder()->CreateLoad(
 251         CreateAddressOfOSRBufEntry(
 252           adjusted_offset(value, max_locals() - 1 - index),
 253           SharkType::to_stackType(value->basic_type()))),
 254       value->zero_checked());
 255   }
 256 }
 257 
 258 void SharkDecacher::write_value_to_frame(Type* type,
 259                                          Value*      value,
 260                                          int         offset) {
 261   builder()->CreateStore(value, stack()->slot_addr(offset, type));
 262 }
 263 
 264 Value* SharkCacher::read_value_from_frame(Type* type, int offset) {
 265   return builder()->CreateLoad(stack()->slot_addr(offset, type));
 266 }