1 /*
   2  * Copyright (c) 2013, Red Hat Inc.
   3  * Copyright (c) 1997, 2010, Oracle and/or its affiliates.
   4  * All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #ifndef CPU_AARCH64_VM_FRAME_AARCH64_INLINE_HPP
  28 #define CPU_AARCH64_VM_FRAME_AARCH64_INLINE_HPP
  29 
  30 #include "code/codeCache.hpp"
  31 
  32 // Inline functions for AArch64 frames:
  33 
  34 // Constructors:
  35 
  36 inline frame::frame() {
  37   _pc = NULL;
  38   _sp = NULL;
  39   _unextended_sp = NULL;
  40   _fp = NULL;
  41   _cb = NULL;
  42   _deopt_state = unknown;
  43 }
  44 
  45 //static int spin;
  46 
  47 inline void frame::init(intptr_t* sp, intptr_t* fp, address pc) {
  48   intptr_t a = intptr_t(sp);
  49   intptr_t b = intptr_t(fp);
  50   _sp = sp;
  51   _unextended_sp = sp;
  52   _fp = fp;
  53   _pc = pc;
  54   assert(pc != NULL, "no pc?");
  55   _cb = CodeCache::find_blob(pc);
  56   adjust_unextended_sp();
  57 
  58   address original_pc = nmethod::get_deopt_original_pc(this);
  59   if (original_pc != NULL) {
  60     _pc = original_pc;
  61     _deopt_state = is_deoptimized;
  62   } else {
  63     _deopt_state = not_deoptimized;
  64   }
  65 }
  66 
  67 inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) {
  68   init(sp, fp, pc);
  69 }
  70 
  71 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc) {
  72   intptr_t a = intptr_t(sp);
  73   intptr_t b = intptr_t(fp);
  74   _sp = sp;
  75   _unextended_sp = unextended_sp;
  76   _fp = fp;
  77   _pc = pc;
  78   assert(pc != NULL, "no pc?");
  79   _cb = CodeCache::find_blob(pc);
  80   adjust_unextended_sp();
  81 
  82   address original_pc = nmethod::get_deopt_original_pc(this);
  83   if (original_pc != NULL) {
  84     _pc = original_pc;
  85     assert(((nmethod*)_cb)->insts_contains(_pc), "original PC must be in nmethod");
  86     _deopt_state = is_deoptimized;
  87   } else {
  88     _deopt_state = not_deoptimized;
  89   }
  90 }
  91 
  92 inline frame::frame(intptr_t* sp, intptr_t* fp) {
  93   intptr_t a = intptr_t(sp);
  94   intptr_t b = intptr_t(fp);
  95   _sp = sp;
  96   _unextended_sp = sp;
  97   _fp = fp;
  98   _pc = (address)(sp[-1]);
  99 
 100   // Here's a sticky one. This constructor can be called via AsyncGetCallTrace
 101   // when last_Java_sp is non-null but the pc fetched is junk. If we are truly
 102   // unlucky the junk value could be to a zombied method and we'll die on the
 103   // find_blob call. This is also why we can have no asserts on the validity
 104   // of the pc we find here. AsyncGetCallTrace -> pd_get_top_frame_for_signal_handler
 105   // -> pd_last_frame should use a specialized version of pd_last_frame which could
 106   // call a specilaized frame constructor instead of this one.
 107   // Then we could use the assert below. However this assert is of somewhat dubious
 108   // value.
 109   // assert(_pc != NULL, "no pc?");
 110 
 111   _cb = CodeCache::find_blob(_pc);
 112   adjust_unextended_sp();
 113 
 114   address original_pc = nmethod::get_deopt_original_pc(this);
 115   if (original_pc != NULL) {
 116     _pc = original_pc;
 117     _deopt_state = is_deoptimized;
 118   } else {
 119     _deopt_state = not_deoptimized;
 120   }
 121 }
 122 
 123 // Accessors
 124 
 125 inline bool frame::equal(frame other) const {
 126   bool ret =  sp() == other.sp()
 127               && unextended_sp() == other.unextended_sp()
 128               && fp() == other.fp()
 129               && pc() == other.pc();
 130   assert(!ret || ret && cb() == other.cb() && _deopt_state == other._deopt_state, "inconsistent construction");
 131   return ret;
 132 }
 133 
 134 // Return unique id for this frame. The id must have a value where we can distinguish
 135 // identity and younger/older relationship. NULL represents an invalid (incomparable)
 136 // frame.
 137 inline intptr_t* frame::id(void) const { return unextended_sp(); }
 138 
 139 // Relationals on frames based
 140 // Return true if the frame is younger (more recent activation) than the frame represented by id
 141 inline bool frame::is_younger(intptr_t* id) const { assert(this->id() != NULL && id != NULL, "NULL frame id");
 142                                                     return this->id() < id ; }
 143 
 144 // Return true if the frame is older (less recent activation) than the frame represented by id
 145 inline bool frame::is_older(intptr_t* id) const   { assert(this->id() != NULL && id != NULL, "NULL frame id");
 146                                                     return this->id() > id ; }
 147 
 148 
 149 
 150 inline intptr_t* frame::link() const              { return (intptr_t*) *(intptr_t **)addr_at(link_offset); }
 151 inline void      frame::set_link(intptr_t* addr)  { *(intptr_t **)addr_at(link_offset) = addr; }
 152 
 153 
 154 inline intptr_t* frame::unextended_sp() const     { return _unextended_sp; }
 155 
 156 // Return address:
 157 
 158 inline address* frame::sender_pc_addr()      const { return (address*) addr_at( return_addr_offset); }
 159 inline address  frame::sender_pc()           const { return *sender_pc_addr(); }
 160 
 161 // return address of param, zero origin index.
 162 inline address* frame::native_param_addr(int idx) const { return (address*) addr_at( native_frame_initial_param_offset+idx); }
 163 
 164 #ifdef CC_INTERP
 165 
 166 inline interpreterState frame::get_interpreterState() const {
 167   return ((interpreterState)addr_at( -((int)sizeof(BytecodeInterpreter))/wordSize ));
 168 }
 169 
 170 inline intptr_t*    frame::sender_sp()        const {
 171   // Hmm this seems awfully expensive QQQ, is this really called with interpreted frames?
 172   if (is_interpreted_frame()) {
 173     assert(false, "should never happen");
 174     return get_interpreterState()->sender_sp();
 175   } else {
 176     return            addr_at(sender_sp_offset);
 177   }
 178 }
 179 
 180 inline intptr_t** frame::interpreter_frame_locals_addr() const {
 181   assert(is_interpreted_frame(), "must be interpreted");
 182   return &(get_interpreterState()->_locals);
 183 }
 184 
 185 inline intptr_t* frame::interpreter_frame_bcx_addr() const {
 186   assert(is_interpreted_frame(), "must be interpreted");
 187   return (intptr_t*) &(get_interpreterState()->_bcp);
 188 }
 189 
 190 
 191 // Constant pool cache
 192 
 193 inline constantPoolCacheOop* frame::interpreter_frame_cache_addr() const {
 194   assert(is_interpreted_frame(), "must be interpreted");
 195   return &(get_interpreterState()->_constants);
 196 }
 197 
 198 // Method
 199 
 200 inline methodOop* frame::interpreter_frame_method_addr() const {
 201   assert(is_interpreted_frame(), "must be interpreted");
 202   return &(get_interpreterState()->_method);
 203 }
 204 
 205 inline intptr_t* frame::interpreter_frame_mdx_addr() const {
 206   assert(is_interpreted_frame(), "must be interpreted");
 207   return (intptr_t*) &(get_interpreterState()->_mdx);
 208 }
 209 
 210 // top of expression stack
 211 inline intptr_t* frame::interpreter_frame_tos_address() const {
 212   assert(is_interpreted_frame(), "wrong frame type");
 213   return get_interpreterState()->_stack + 1;
 214 }
 215 
 216 #else /* asm interpreter */
 217 inline intptr_t*    frame::sender_sp()        const { return            addr_at(   sender_sp_offset); }
 218 
 219 inline intptr_t** frame::interpreter_frame_locals_addr() const {
 220   return (intptr_t**)addr_at(interpreter_frame_locals_offset);
 221 }
 222 
 223 inline intptr_t* frame::interpreter_frame_last_sp() const {
 224   return *(intptr_t**)addr_at(interpreter_frame_last_sp_offset);
 225 }
 226 
 227 inline intptr_t* frame::interpreter_frame_bcx_addr() const {
 228   return (intptr_t*)addr_at(interpreter_frame_bcx_offset);
 229 }
 230 
 231 
 232 inline intptr_t* frame::interpreter_frame_mdx_addr() const {
 233   return (intptr_t*)addr_at(interpreter_frame_mdx_offset);
 234 }
 235 
 236 
 237 
 238 // Constant pool cache
 239 
 240 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
 241   return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
 242 }
 243 
 244 // Method
 245 
 246 inline Method** frame::interpreter_frame_method_addr() const {
 247   return (Method**)addr_at(interpreter_frame_method_offset);
 248 }
 249 
 250 // top of expression stack
 251 inline intptr_t* frame::interpreter_frame_tos_address() const {
 252   intptr_t* last_sp = interpreter_frame_last_sp();
 253   if (last_sp == NULL) {
 254     return sp();
 255   } else {
 256     // sp() may have been extended or shrunk by an adapter.  At least
 257     // check that we don't fall behind the legal region.
 258     // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
 259     assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end(), "bad tos");
 260     return last_sp;
 261   }
 262 }
 263 
 264 inline oop* frame::interpreter_frame_temp_oop_addr() const {
 265   return (oop *)(fp() + interpreter_frame_oop_temp_offset);
 266 }
 267 
 268 #endif /* CC_INTERP */
 269 
 270 inline int frame::pd_oop_map_offset_adjustment() const {
 271   return 0;
 272 }
 273 
 274 inline int frame::interpreter_frame_monitor_size() {
 275   return BasicObjectLock::size();
 276 }
 277 
 278 
 279 // expression stack
 280 // (the max_stack arguments are used by the GC; see class FrameClosure)
 281 
 282 inline intptr_t* frame::interpreter_frame_expression_stack() const {
 283   intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
 284   return monitor_end-1;
 285 }
 286 
 287 
 288 inline jint frame::interpreter_frame_expression_stack_direction() { return -1; }
 289 
 290 
 291 // Entry frames
 292 
 293 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
 294  return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
 295 }
 296 
 297 
 298 // Compiled frames
 299 
 300 inline int frame::local_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors) {
 301   return (nof_args - local_index + (local_index < nof_args ? 1: -1));
 302 }
 303 
 304 inline int frame::monitor_offset_for_compiler(int local_index, int nof_args, int max_nof_locals, int max_nof_monitors) {
 305   return local_offset_for_compiler(local_index, nof_args, max_nof_locals, max_nof_monitors);
 306 }
 307 
 308 inline int frame::min_local_offset_for_compiler(int nof_args, int max_nof_locals, int max_nof_monitors) {
 309   return (nof_args - (max_nof_locals + max_nof_monitors*2) - 1);
 310 }
 311 
 312 inline bool frame::volatile_across_calls(Register reg) {
 313   return true;
 314 }
 315 
 316 
 317 
 318 inline oop frame::saved_oop_result(RegisterMap* map) const {
 319   oop* result_adr = (oop *)map->location(r0->as_VMReg());
 320   guarantee(result_adr != NULL, "bad register save location");
 321 
 322   return (*result_adr);
 323 }
 324 
 325 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
 326   oop* result_adr = (oop *)map->location(r0->as_VMReg());
 327   guarantee(result_adr != NULL, "bad register save location");
 328 
 329   *result_adr = obj;
 330 }
 331 
 332 #endif // CPU_AARCH64_VM_FRAME_AARCH64_INLINE_HPP