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