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