1 /*
   2  * Copyright (c) 1997, 2017, 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 "code/debugInfo.hpp"
  27 #include "oops/compressedOops.inline.hpp"
  28 #include "oops/oop.hpp"
  29 #include "runtime/frame.inline.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 #include "runtime/stackValue.hpp"
  32 #if INCLUDE_ZGC
  33 #include "gc/z/zBarrier.inline.hpp"
  34 #endif
  35 #if INCLUDE_SHENANDOAHGC
  36 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  37 #endif
  38 
  39 StackValue* StackValue::create_stack_value(ScopeValue* sv, address value_addr, bool in_cont) {
  40   if (sv->is_location()) {
  41     // Stack or register value
  42     Location loc = ((LocationValue *)sv)->location();
  43 
  44 #ifdef SPARC
  45     // %%%%% Callee-save floats will NOT be working on a Sparc until we
  46     // handle the case of a 2 floats in a single double register.
  47     assert( !(loc.is_register() && loc.type() == Location::float_in_dbl), "Sparc does not handle callee-save floats yet" );
  48 #endif // SPARC
  49 
  50     // Then package it right depending on type
  51     // Note: the transfer of the data is thru a union that contains
  52     // an intptr_t. This is because an interpreter stack slot is
  53     // really an intptr_t. The use of a union containing an intptr_t
  54     // ensures that on a 64 bit platform we have proper alignment
  55     // and that we store the value where the interpreter will expect
  56     // to find it (i.e. proper endian). Similarly on a 32bit platform
  57     // using the intptr_t ensures that when a value is larger than
  58     // a stack slot (jlong/jdouble) that we capture the proper part
  59     // of the value for the stack slot in question.
  60     //
  61     switch( loc.type() ) {
  62     case Location::float_in_dbl: { // Holds a float in a double register?
  63       // The callee has no clue whether the register holds a float,
  64       // double or is unused.  He always saves a double.  Here we know
  65       // a double was saved, but we only want a float back.  Narrow the
  66       // saved double to the float that the JVM wants.
  67       assert( loc.is_register(), "floats always saved to stack in 1 word" );
  68       union { intptr_t p; jfloat jf; } value;
  69       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
  70       value.jf = (jfloat) *(jdouble*) value_addr;
  71       return new StackValue(value.p); // 64-bit high half is stack junk
  72     }
  73     case Location::int_in_long: { // Holds an int in a long register?
  74       // The callee has no clue whether the register holds an int,
  75       // long or is unused.  He always saves a long.  Here we know
  76       // a long was saved, but we only want an int back.  Narrow the
  77       // saved long to the int that the JVM wants.
  78       assert( loc.is_register(), "ints always saved to stack in 1 word" );
  79       union { intptr_t p; jint ji;} value;
  80       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
  81       value.ji = (jint) *(jlong*) value_addr;
  82       return new StackValue(value.p); // 64-bit high half is stack junk
  83     }
  84 #ifdef _LP64
  85     case Location::dbl:
  86       // Double value in an aligned adjacent pair
  87       return new StackValue(*(intptr_t*)value_addr);
  88     case Location::lng:
  89       // Long   value in an aligned adjacent pair
  90       return new StackValue(*(intptr_t*)value_addr);
  91     case Location::narrowoop: {
  92       assert (UseCompressedOops, "");
  93       union { intptr_t p; narrowOop noop;} value;
  94       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
  95       if (loc.is_register()) {
  96         // The callee has no clue whether the register holds an int,
  97         // long or is unused.  He always saves a long.  Here we know
  98         // a long was saved, but we only want an int back.  Narrow the
  99         // saved long to the int that the JVM wants.
 100         value.noop =  (narrowOop) *(julong*) value_addr;
 101       } else {
 102         value.noop = *(narrowOop*) value_addr;
 103       }
 104       // Decode narrowoop
 105       oop val = CompressedOops::decode(value.noop);
 106       // Deoptimization must make sure all oops have passed load barriers
 107 #if INCLUDE_SHENANDOAHGC
 108       if (UseShenandoahGC) {
 109         val = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(val);
 110       }
 111 #endif
 112       Handle h(Thread::current(), val); // Wrap a handle around the oop
 113       return new StackValue(h);
 114     }
 115 #endif
 116     case Location::oop: {
 117       if (in_cont && UseCompressedOops) {
 118         narrowOop noop = *(narrowOop*) value_addr;
 119         Handle h(Thread::current(), CompressedOops::decode(noop));
 120         return new StackValue(h);
 121       } 
 122       
 123       oop val = *(oop *)value_addr;
 124 #ifdef _LP64
 125       if (CompressedOops::is_base(val)) {
 126          // Compiled code may produce decoded oop = narrow_oop_base
 127          // when a narrow oop implicit null check is used.
 128          // The narrow_oop_base could be NULL or be the address
 129          // of the page below heap. Use NULL value for both cases.
 130          val = (oop)NULL;
 131       }
 132 #endif
 133       // Deoptimization must make sure all oops have passed load barriers
 134 #if INCLUDE_SHENANDOAHGC
 135       if (UseShenandoahGC) {
 136         val = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(val);
 137       }
 138 #endif
 139       assert(oopDesc::is_oop_or_null(val, false), "bad oop found");
 140       Handle h(Thread::current(), val); // Wrap a handle around the oop
 141       return new StackValue(h);
 142     }
 143     case Location::addr: {
 144       ShouldNotReachHere(); // both C1 and C2 now inline jsrs
 145     }
 146     case Location::normal: {
 147       // Just copy all other bits straight through
 148       union { intptr_t p; jint ji;} value;
 149       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 150       value.ji = *(jint*)value_addr;
 151       return new StackValue(value.p);
 152     }
 153     case Location::invalid:
 154       return new StackValue();
 155     default:
 156       ShouldNotReachHere();
 157     }
 158 
 159   } else if (sv->is_constant_int()) {
 160     // Constant int: treat same as register int.
 161     union { intptr_t p; jint ji;} value;
 162     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 163     value.ji = (jint)((ConstantIntValue*)sv)->value();
 164     return new StackValue(value.p);
 165   } else if (sv->is_constant_oop()) {
 166     // constant oop
 167     return new StackValue(sv->as_ConstantOopReadValue()->value());
 168 #ifdef _LP64
 169   } else if (sv->is_constant_double()) {
 170     // Constant double in a single stack slot
 171     union { intptr_t p; double d; } value;
 172     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 173     value.d = ((ConstantDoubleValue *)sv)->value();
 174     return new StackValue(value.p);
 175   } else if (sv->is_constant_long()) {
 176     // Constant long in a single stack slot
 177     union { intptr_t p; jlong jl; } value;
 178     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 179     value.jl = ((ConstantLongValue *)sv)->value();
 180     return new StackValue(value.p);
 181 #endif
 182   } else if (sv->is_object()) { // Scalar replaced object in compiled frame
 183     Handle ov = ((ObjectValue *)sv)->value();
 184     return new StackValue(ov, (ov.is_null()) ? 1 : 0);
 185   }
 186 
 187   // Unknown ScopeValue type
 188   ShouldNotReachHere();
 189   return new StackValue((intptr_t) 0);   // dummy
 190 }
 191 
 192 
 193 BasicLock* StackValue::resolve_monitor_lock(const frame* fr, Location location) {
 194   assert(location.is_stack(), "for now we only look at the stack");
 195   int word_offset = location.stack_offset() / wordSize;
 196   // (stack picture)
 197   // high: [     ]  word_offset + 1
 198   // low   [     ]  word_offset
 199   //
 200   // sp->  [     ]  0
 201   // the word_offset is the distance from the stack pointer to the lowest address
 202   // The frame's original stack pointer, before any extension by its callee
 203   // (due to Compiler1 linkage on SPARC), must be used.
 204   return (BasicLock*) (fr->unextended_sp() + word_offset);
 205 }
 206 
 207 
 208 #ifndef PRODUCT
 209 
 210 void StackValue::print_on(outputStream* st) const {
 211   switch(_type) {
 212     case T_INT:
 213       st->print("%d (int) %f (float) %x (hex)",  *(int *)&_integer_value, *(float *)&_integer_value,  *(int *)&_integer_value);
 214       break;
 215 
 216     case T_OBJECT:
 217       if (_handle_value() != NULL) {
 218         _handle_value()->print_value_on(st);
 219       } else {
 220         st->print("NULL");
 221       }
 222       st->print(" <" INTPTR_FORMAT ">", p2i((address)_handle_value()));
 223      break;
 224 
 225     case T_CONFLICT:
 226      st->print("conflict");
 227      break;
 228 
 229     default:
 230      ShouldNotReachHere();
 231   }
 232 }
 233 
 234 #endif