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